Get a list of numbers as input from the user

前端 未结 17 1352
生来不讨喜
生来不讨喜 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:15

    eval(a_string) evaluates a string as Python code. Obviously this is not particularly safe. You can get safer (more restricted) evaluation by using the literal_eval function from the ast module.

    raw_input() is called that in Python 2.x because it gets raw, not "interpreted" input. input() interprets the input, i.e. is equivalent to eval(raw_input()).

    In Python 3.x, input() does what raw_input() used to do, and you must evaluate the contents manually if that's what you want (i.e. eval(input())).

提交回复
热议问题