【发布时间】:2015-07-12 07:20:21
【问题描述】:
下面有一段包含while循环的小代码。
question = raw_input("How are you? > ")
state = True
number = 0
print "Hello"
while True:
if question == "good":
print "Ok. Your mood is good."
state = False
question_2 = raw_input("How are you 2? > ")
elif question == "normal":
print "Ok. Your mood is normal."
elif question == "bad":
print "It's bad. Do an interesting activity, return and say again what your mood is."
else:
print "Nothing"
如果我输入 "normal",程序会打印 Ok。你的心情很正常。无数次。
但如果我输入 "good",程序会打印 Ok。你的心情正常。并打印question_2的内容。
为什么question_2 = raw_input("How are you 2? > ") 中的问题没有重复无数次?
判断raw_input() 停止任何无限while循环是否合理?
【问题讨论】:
-
听起来很奇怪,你确定是
while True:而不是while state:? -
是的。我还没有结束这段代码,状态是为了其他步骤。
标签: python while-loop infinite-loop