【发布时间】:2018-03-12 02:52:34
【问题描述】:
我已经尝试了此代码的两个版本,但最终都给了我不同的结果。有什么原因吗?
Raw_input() 使程序进入无限循环; input() 正常工作
numbers = []
increments = 1
def loop(i, dial):
while i < dial:
print "At the top i is %d" % i
numbers.append(i)
i += increments
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
return
dial = raw_input('What is dial? ') # -Code goes berserck
#OR **dial = input('What is dial? ') -Code acts normal
loop(0, dial)
【问题讨论】:
-
它们在 2.7 中完全不同 docs。
-
input()是您希望在 Python 2 中避免使用的函数;使用raw_input()并进行适当的转换(在您的情况下为整数)。 -
此外,您的
dial变量是str。所以当然你的while永远不会评估终止。 -
在询问之前您可以在交互式提示下输入
help(input)和help(raw_input)。或者您可以在文档的索引中查找两者。学习同时使用help和索引。
标签: python python-2.7 input