Get a list of numbers as input from the user

前端 未结 17 1146
生来不讨喜
生来不讨喜 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)
    
    >>>
    

提交回复
热议问题