Get a list of numbers as input from the user

前端 未结 17 1355
生来不讨喜
生来不讨喜 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 23:13

    You can use .split()

    numbers = raw_input().split(",")
    print len(numbers)
    

    This will still give you strings, but it will be a list of strings.

    If you need to map them to a type, use list comprehension:

    numbers = [int(n, 10) for n in raw_input().split(",")]
    print len(numbers)
    

    If you want to be able to enter in any Python type and have it mapped automatically and you trust your users IMPLICITLY then you can use eval

提交回复
热议问题