1

Whenever I run this I get the third option when it should be returning the first, since s = 'yes'. What is going wrong here?

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)
Pavel Anossov
  • 57,586
  • 14
  • 141
  • 121
jumbopap
  • 3,734
  • 2
  • 23
  • 45

2 Answers2

5

Change

if s is 'yes':

to

if s == 'yes':

and

elif s is 'no':

to

elif s == 'no':

While is is a valid operator, it is not the one to use here (it compares object identity instead of comparing the character sequences).

NPE
  • 464,258
  • 100
  • 912
  • 987
4

is tests for identity, not equality. To test if a string is equal to yes use s=='yes'

Ivaylo Strandjev
  • 66,530
  • 15
  • 117
  • 170