为什么条件“ else”在我的python代码中不起作用 - python

这是我的代码。

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)+" ."

每次键入输入内容时,都会打印出“抱歉,我不明白您的输入内容”。条件“其他”不起作用。

我不知道为什么有人可以帮我吗?非常感谢你!

python大神给出的解决方案

因为按照书面规定,每个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."