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
  • 60,842
  • 14
  • 151
  • 124
jumbopap
  • 3,969
  • 5
  • 27
  • 47

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
  • 486,780
  • 108
  • 951
  • 1,012
4

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

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176