Functions and if - else in python. Mutliple conditions. Codeacademy
This:
s == "Yes" or "yes" or "YES"
is equivalent to this:
(s == "Yes") or ("yes") or ("YES")
Which will always return True
, since a non-empty string is True
.
Instead, you want to compare s
with each string individually, like so:
(s == "Yes") or (s == "yes") or (s == "YES") # brackets just for clarification
It should end up like this:
def shut_down(s):
if s == "Yes" or s == "yes" or s == "YES":
return "Shutting down..."
elif s == "No" or s == "no" or s == "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
You can do it a couple of ways:
if s == 'Yes' or s == 'yes' or s == 'YES':
return "Shutting down..."
Or:
if s in ['Yes', 'yes', 'YES']:
return "Shutting down..."
Welcome to SO. I am going to walk through the answer, step-by-step.
s = raw_input ("Would you like to shut down?")
This asks if the user would like to shut down.
def shut_down(s):
if s.lower() == "yes":
print "Shutting down..."
elif s.lower() == "no":
print "Shutdown aborted!"
else:
print "Sorry, I didn't understand you."
This is probably new to you. If you have a string, and then .lower()
it changes all input from s
to lowercase. This is simpler than giving a list of all possibilities.
shut_down(s)
This calls the function.