Write a function, shut_down
, that takes one parameter (you can use anything you like; in this case, we\'d use s
for string).
The shut_down func
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.
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."
I'm a python programmer and have finished Codecademy. I see that you have a problem and let me give you my answer. It runs perfectly
def shut_down(s):
if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"
The code from the user 'grc' posted here, almost worked for me. I had to tweak the return message to get it right. If the message (meaning all returned strings) are not exactly the same as described on Codecademy, then the workspace will not validate your response.
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 know this doesn't exactly fit the specification but this is another common option which would catch a few more permutations:
def shut_down(s):
s = s.upper()
if s == "YES":
return "Shutting down..."
elif s == "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
def shut_down(phrase):
word = phrase
return word
take_action = input(shut_down('do you want to shutdown the program?: '.title()))
if take_action.lower() == 'yes':
print('Shutting down...')
elif take_action.lower() == 'no':
print('Shutdown aborted!')
else:
print('Sorry, I didn\'t understand you.')