简体   繁体   中英

Functions and if - else in python. Mutliple conditions. Codeacademy

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, eg 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM