elif块没有在Python中执行

时间:2015-03-10 22:46:14

标签: 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?"

2 个答案:

答案 0 :(得分:12)

raw_input返回的字符串不是整数,例如'3'代替3

在Python 2中,字符串总是比大于整数,因此weather >= 5始终为真。

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

weather = int(raw_input('How is the weather today, from 0 to 10? '))

答案 1 :(得分:2)

raw_input返回一个字符串,您需要将其转换为整数。

weather = int(raw_input('How is the weather today, from 0 to 10? '))

您需要捕获处理错误输入的任何异常。