elif块没有在Python中执行

sup*_*ova 2 python if-statement

这有什么不对?在输入3(<= 5)时,输出应该是,我认为,"Phew,......"但它仍然是"很棒,......"

weather = raw_input('How is the weather today, from 0 to 10? ')
answer = raw_input ('Are you in mood to go out? ')
if weather >= 5:
    if answer == 'Yes':
        print 'Awesome, I will go out!'
        print 'Will you come with me?'
    else:
        print 'Awesome, I am going out now!'
elif weather <= 5:
    print "Phew, I didn't want to go outside, great, I will stay in! Thank God!"
else:
    print "Huh?"
Run Code Online (Sandbox Code Playgroud)

Ale*_*ley 12

raw_input返回一个不是整数的字符串,例如,'3'而不是3.

在Python 2中,字符串总是比整数大,所以weather >= 5总是如此.

要解决此问题,请weather转换为整数:

weather = int(raw_input('How is the weather today, from 0 to 10? '))
Run Code Online (Sandbox Code Playgroud)