我正在使用for循环来制作一个超级简单的程序,它使用for循环三次询问您的爱好,并将您的答案附加到一个名为“爱好”的列表中:
hobbies = []
for me in range(3):
hobby=input("Tell me one of your hobbies: ")
hobbies.append(hobby)
例如,如果我给它“编码”,它将返回:
Traceback (most recent call last):
File "python", line 4, in <module>
File "<string>", line 1, in <module>
NameError: name 'coding' is not defined
请注意,如果我使用python2.7并改用raw_input
,那么这个程序运行得很好。你知道吗
在python2中,
input
将计算给定的字符串,而raw_input
将只返回一个字符串。注意,在python3中,raw_input
被重命名为input
,旧的input
仅以eval(input())
的形式可用。你知道吗Python 2中的示例:
相关问题 更多 >
编程相关推荐