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 function should return "Shutting down..." when it gets "Yes", "yes", or "YES" as an argument, and "Shutdown aborted!" when it gets "No", "no", or "NO".
If it gets anything other than those inputs, the function should return "Sorry, I didn't understand you."
The code I wrote so far is below. It makes errors, e.g. given "No" as the argument, it does not return "Shutdown aborted!" as expected.
def shut_down(s):
if s == "Yes" or "yes" or "YES":
return "Shutting down..."
elif s == "No" or "no" or "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
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.
def shut_down(s):
return ("Shutting down..." if s in("Yes","yes","YES")
else "Shutdown aborted!" if s in ("No","no","NO")
else "Sorry, I didn't understand you.")
GordonsBeard's idea is a good one. Probably "yEs" and "yES" etc are acceptable criteria;
Then I propose in this case:
def shut_down(s,d = {'yes':"Shutting down...",'no':"Shutdown aborted!"}):
return d.get(s.lower(),"Sorry, I didn't understand you.")
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.')
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"
You can try this code:
def shut_down(s):
if s =="yes":
return "Shutting Down"
elif s =="no":
return "Shutdown aborted"
else:
return "Sorry"
print shut_down("yes")
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"
Related
Here is my pyhton practice program:
def shut_down(s):
return s
if yes():
shut_down("Shutting down")
elif no():
shut_down("Shutdown aborted")
else:
shut_down("Sorry")
the question given to me are:
if the shut_down function receives an s equal to "yes", it should return "Shutting down"
elif s is equal to "no", then the function should return "Shutdown aborted".
if shut_down gets anything other than those inputs, the function should return "Sorry"
Couple of points:
Misplaced return statement. Should be at end.
if yes(): It is wrong. You want to compare function input with yes. It should be if s == 'yes':. Same for rest also.
Since you have written function definition as def shut_down(s):, it is expecting one argument. You should pass one argument while calling this function as shutdown(yes)
Once you called function and since your function has return statement, it will return some value, which you should catch like ret = shutdown(yes)
def shut_down(s):
if s == "yes":
r = "Shutting down"
elif s == "no":
r = "Shutdown aborted"
else:
r = "Sorry"
return r
ret = shut_down("yes")
print (ret)
something shorter than the best answer:
def shut_down(s):
if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"
print shut_down("algo")
def shut_down(s):
if s == "Yes":
return s == "Shutting down"
elif s == "No":
return s == "Shutdown aborted"
else:
return s == "Sorry"
As others pointed out in comments your return should at the logical end of your function.
def shut_down(s):
if s == "yes":
r = "Shutting down"
elif s == "no":
r = "Shutdown aborted"
else:
r = "Sorry"
return r
I am trying to learn Python for fun. I'm doing an online lesson at codecademy.com, but I have no idea what I have to do.
I'm trying to compare 's' parameter with other parameter (string parameter), and the returned value should be "Sorry".
def shut_down(s):
return s
if s == 'yes':
return "Shutting down"
elif s == 'no':
return "Shutdown aborted"
else:
return "Sorry"
so I call the function:
shut_down('lhagvbs')
It always returns, "Your function failed on the message yes. It returned 'yes' when it should have returned 'Shutting down'"
And i think my code does not work. Not even the def function is correct.
Please explain why, what, where, etc.
Sorry, I do not speak/write English well, but I hope it's understandable.
(This is not homework).
You need to remove the return s as you are returning s regardless, all the code after return s is unreachable so you never evaluate your if/elif or else:
def shut_down(s):
if s == 'yes':
return "Shutting down"
elif s == 'no':
return "Shutdown aborted"
else:
return "Sorry"
In keeping with the fact you can only return once from any function, you can also forget the elif and else:
def shut_down(s):
if s == 'yes':
return "Shutting down"
if s == 'no':
return "Shutdown aborted"
return "Sorry"
I don't understand why this function doesn't print the text I want it to print, what could I change?
def shut_down(answer):
if answer == "Yes" or "YES" or "yes":
print "Shutting down..."
return "Shutting down..."
if answer == "No"or "NO" or "no":
print "Shutdown aborted!"
return "Shutdown aborted!"
else:
print "Sorry, I didn't understand you."
return "Sorry, I didn't understand you."
shut_down(yes)
I guess the language is Python. The or operator operate between conditions, so you need to do
answer == "yes" OR answer == "YES"
etc ....
Your answer == "Yes" Or "YES" is always true because "YES" is considered as an true condition.
if answer.lower() == 'yes':
That's how you should do it. Actually, do this: if answer.lower() in ('yes', 'y'): to make it more convenient for lazy people like myself.
Anyway, the proper way to check for a list of strings is this:
if answer in ('foo', 'bar', 'foobar'):
The following is not correct (the body of the if always executes):
if answer == "Yes" or "YES" or "yes":
Instead, write
if answer in {"Yes", "YES", "yes"}:
or
if answer.lower() == "yes":
You want to change it to this:
if answer == "Yes" or answer == "YES" or answer == "yes":
print "Shutting down..."
return "Shutting down..."
Or even tidier:
if answer in ["Yes", "YES", "yes"]:
or even moar tidier:
if answer.lower() == "yes":
You code probably has an error because you do not have the variable yes defined anywhere.
Define it, and then you should be good.
I am talking about the variable you are passing to your shut_down function
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed last year.
I'm learning python on codecademy and my current task is this:
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
function should return "Shutting down..." when it gets "Yes", "yes",
or "YES" as an argument, and "Shutdown aborted!" when it gets "No",
"no", or "NO".
If it gets anything other than those inputs, the function should
return "Sorry, I didn't understand you."
Seemed easy to me but somehow I still can't do it.
My code I made to test the function:
def shut_down(s):
if s == "Yes" or s == "yes" or s == "YES":
return "Shutting down..."
elif s == "No" or "no" or "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
i = input("Do you want to shutdown?")
print(i) #was to test the input
print(shut_down(i)) #never returns "Sorry, I didn't understand you"
It works fine for the no's and yes', but somehow if I put a space before any yes or even if I just type in "a" it prints "Shutdown aborted!" although it should print "Sorry, I didn't understand you".
What am I doing wrong?
You forgot to write s == "no" in your first elif:
def shut_down(s):
if s == "Yes" or s == "yes" or s == "YES":
return "Shutting down..."
elif s == "No" or "no" or "NO": # you forgot the s== in this line
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
Do 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": # fixed it
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
This is because:
elif s == "No" or "no" or "NO": #<---this
elif s == "No" or True or True: #<---is the same as this
Since this is the accepted answer I'll elaborate to include standard practices: The convention for comparing strings regardless of capitalization (equalsIgnoreCase) is to use .lower() like this
elif s.lower() == "no":
Instead of checking for different combinations of capitalization you could uses the lower function to return a copy of s in lowercase and compare against that.
def shut_down(s):
if s.lower() == "yes":
return "Shutting down..."
elif s.lower() == "no":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
This is much cleaner and easier to debug. Alternatively you could use upper also and compare against "YES" and "NO".
If this doesn't help because of matching cases like nO then I'd go with the in statement:
def shut_down(s):
if s in ("yes","Yes","YES"):
return "Shutting down..."
elif s in ("no","No","NO"):
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
Python evaluates non empty strings to be True, so your elif condition is always evaluated to True.
>>> bool('No')
True
>>> bool('NO')
True
Doing a boolean or with a True value would always return True, so it never reaches the else condition and gets stuck on the elif one.
You need to test the conditions using.
elif choice == 'no' or choice == 'NO' or choice == 'No':
EDIT - As glglgl pointed out in the comment, == binds harder than or, so your condition gets evaluated as (s == 'No') or 'no' or 'NO' and not s == ('No' or 'no' or 'NO'), in which case you would have gotten to the else part even for a user input of 'NO'.
from sys import exit
def answer():
answer = raw_input("> ")
if answer == "Yes" or answer == "yes":
#going to next
joint()
elif answer == "No" or answer == "no":
print "You still have something, I know..."
again()
else:
fubar()
def again():
again = raw_input("> ")
if again == "Yes" or again == "yes":
#going to next
joint()
elif again == "No" or again == "no":
print "You still have something, I know..."
else:
fubar()
def fuck():
print "Fubar'd!"
def joint():
print "To be continue..."
def question():
print "Hi duuuude..."
raw_input("To say 'Hi' press Enter")
print "Can you help me?"
answer()
question()
Hi, can you help me with this? I`m trying to repeat the function "answer", when I get answer "NO". Im want to escape function "again"... And also is there a way to escape "answer == "Yes" or answer == "yes": " so no matter I write capital or small letter to accept the answer and not to write like a noob "Yes" or "yes"?
This is usually achieved with a while loop.
Edit: As pointed out, while loops are nice and clear, and avoid recursion limits.
Never thought a simple answer would generate so many votes....
Lets give you an example
while True:
ans = raw_input("Enter only y or n to continue").strip().lower()
if ans == "y":
print "Done!"
break
elif ans == "n":
print "No?"
else:
print "Not valid input."
The simplest solution to your problem is remove your again function, and recurse:
def answer():
ans = raw_input("> ")
if ans == "Yes" or ans == "yes":
#going to next
joint()
elif ans == "No" or ans == "no":
print "You still have something, I know..."
answer() # again()
else:
fubar()
I had to rename your answer variable to ans so that it didn't clash with the function name.
For the second question, you want either:
if answer.lower() == "yes":
or
if answer in ("Yes", "yes"):