为什么我的条件返回“无?”

时间:2013-03-16 22:52:36

标签: python

每当我运行它时,我会得到第三个选项,因为它应该返回第一个,因为s ='yes'。这里出了什么问题?

def shut_down(s):
    if s is 'yes':
        return 'Shutting down...'
    elif s is 'no':
        return 'Shutdown aborted!'
    else:
        return "Sorry, I didn't understand you"

ans = 'Yes'
s = ans.lower()
shut_down(s)

2 个答案:

答案 0 :(得分:5)

更改

if s is 'yes':

if s == 'yes':

elif s is 'no':

elif s == 'no':

is is a valid operator时,它不是在这里使用的(它比较对象身份而不是比较字符序列)。

答案 1 :(得分:4)

is测试身份,而不是平等。要测试字符串是否等于 yes,请使用s=='yes'