所以我得到了这段代码,它猜到了0到100之间的数字。 它会提示一个号码,用户回答“' h' '升'或者' c'
现在一切正常,只是我希望这条线只显示当用户输入不等于' h',' l'或者' c'相反,即使我进入' c'这会破坏循环,对吗?
print "Sorry, I did not understand your input."
完整代码
start = 0
end = 100
guess = int((start+end)/2.0)
print "Please think of a number between 0 and 100!"
print
x= 'n'
while x != 'c':
print 'Is your secret number ' + str(guess) + '?'
x = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.").lower()
if x == 'h':
end = guess
elif x == 'l':
start = guess
else:
print "Sorry, I did not understand your input."
guess = int((start + end)/2)
print 'your answer is: ' + str(guess)
所以要澄清这是输出,即使我输入' c'
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.c
Sorry, I did not understand your input.
你的答案是:50
答案 0 :(得分:0)
elif
案例没有'c'
,因此属于最终else
。
添加,以下处理该情况并打破while循环:
elif x == 'c':
break
此外,您可以执行while True:
并循环直到c
休息。
您的更高/更低逻辑也是向后的。例如,对于h
,start = guess。
guess
的更新应该在while循环中缩进。