Python 为什么我的条件返回“无”
每当我运行它时,我都会得到第三个选项,而它应该返回第一个选项,因为s='yes'。这里出了什么问题Python 为什么我的条件返回“无”,python,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 =
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)
改变
到
及
到
虽然是,但这里不使用它,它比较对象标识,而不是比较字符序列。更改
if s is 'yes':
到
及
到
虽然是,但这里不使用它,它比较对象标识,而不是比较字符序列。是测试标识,而不是相等性。要测试字符串是否等于yes,请使用s=='yes'测试的是标识,而不是相等。要测试字符串是否等于yes,请使用s==“yes”这是的重复,这是的重复
if s == 'yes':
elif s is 'no':
elif s == 'no':