这是我的代码。
highnum=100
lownum=0
guessnum=highnum/2
print "Please think of a number between 0 and 100!"
while True:
print "Is your secret number is "+str(guessnum)+"?"
print "Enter 'h' to indicate the guess is too high.",
print "Enter 'l' to indicate the guess is too low. ",
print "Enter 'c' to indicate I guessed correctly."
result=raw_input()
if result=="h":
highnum=guessnum
guessnum=int(highnum+lownum)/2
if result=="l":
lownum=guessnum
guessnum=int(highnum+lownum)/2
if result=="c":
break
else:
print "Sorry, I did not understand your input."
print "Game over. Your secret number was: "+str(guessnum)+" ."
每当我输入输入时,它会输出"抱歉,我不理解您的输入。"条件"否则"不起作用。
我不知道为什么。谁能帮助我?非常感谢你!
答案 0 :(得分:5)
因为如上所述,您的每个if
语句都是独立的,else
只对应您的上一个if result == 'c'
,因此如果他们不输入'c'
,他们打了你的else
案件。
相反,您可以使用if/elif/else
来尝试每个案例。
if result=="h":
highnum=guessnum
guessnum=int(highnum+lownum)/2
elif result=="l":
lownum=guessnum
guessnum=int(highnum+lownum)/2
elif result=="c":
break
else:
print "Sorry, I did not understand your input."