1

每当我运行它时,我都会得到第三个选项,因为它应该返回第一个选项,因为 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)
4

2 回答 2

5

改变

if s is 'yes':

if s == 'yes':

elif s is 'no':

elif s == 'no':

Whileis 是一个有效的操作符,它不是在这里使用的(它比较对象身份而不是比较字符序列)。

于 2013-03-16T22:55:11.527 回答
4

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

于 2013-03-16T22:55:24.863 回答