【发布时间】:2016-06-20 16:55:52
【问题描述】:
所以我得到了这个代码,它会猜测 0 到 100 之间的数字。 它提示一个数字,用户用'h''l'或'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
【问题讨论】:
标签: python-2.7 while-loop