Get a list of numbers as input from the user

前端 未结 17 1150
生来不讨喜
生来不讨喜 2020-11-21 22:26

I tried to use raw_input() to get a list of numbers, however with the code

numbers = raw_input()
print len(numbers)

the input

相关标签:
17条回答
  • 2020-11-21 22:58

    In Python 3 :

    input_list = [int(x.strip()) for x in input("enter list:").strip()[1:-1].split(",")]
    

    It will ask to "enter list" so just enter list like [2,4,5]

    (common_py3) PS E:\virtual_env_all\common_py3\Scripts> python
    Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> input_list = [int(x.strip()) for x in input("enter list:").strip()[1:-1].split(",")]
    enter list:[2,4,5]
    >>> input_list
    [2, 4, 5]
    >>> type(input_list)
    <class 'list'>
    >>>
    
    0 讨论(0)
  • 2020-11-21 22:59

    Get a list of number as input from the user.

    This can be done by using list in python.

    L=list(map(int,input(),split()))
    

    Here L indicates list, map is used to map input with the position, int specifies the datatype of the user input which is in integer datatype, and split() is used to split the number based on space.

    .

    0 讨论(0)
  • 2020-11-21 22:59

    You just need to typeraw_input().split() and the default split() is that values are split by a whitespace.

    0 讨论(0)
  • 2020-11-21 23:03
    a=[]
    b=int(input())
    for i in range(b):
        c=int(input())
        a.append(c)
    

    The above code snippets is easy method to get values from the user.

    0 讨论(0)
  • 2020-11-21 23:04

    Try this:

    numbers = raw_input()
    numberlist = list(numbers)
    
    0 讨论(0)
  • 2020-11-21 23:06

    Answer is trivial. try this.

    x=input()

    Suppose that [1,3,5,'aA','8as'] are given as the inputs

    print len(x)

    this gives an answer of 5

    print x[3]

    this gives 'aA'

    0 讨论(0)
提交回复
热议问题