脚本:
#!/usr/bin/python
for i in range(5):
value = input ("Enter the value : ")
print value
而字符串作为输入:
$ ./fun+for.py
Enter the value : John
Traceback (most recent call last):
File "./fun+for.py", line 13, in <module>
value = input ("Enter the value : ")
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
而整数输入:
$ ./fun+for.py
Enter the value : 4
4
Enter the value : 5
5
Enter the value : 6
6
Enter the value : 7
7
Enter the value : 7
7
发布于 2018-03-28 23:24:43
因为在Python2中,您输入的字符串实际上要经过eval(),所以它将John
作为变量名进行搜索(尝试输入2+1
)。使用raw_input()
而不是input()
(在python3中,input()
不再这样做)。
发布于 2018-04-04 14:28:14
这是因为它是python 2;如果您在python 3中复制和粘贴这段代码,它就会工作--我认为这更好。您将能够输入字符串、整数和双倍,而不会收到错误。
for i in range(5):
value = input ("Enter the value : ")
print (value)
..hope这个有帮助。
https://stackoverflow.com/questions/49561150
复制