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

后端 未结 9 1421
-上瘾入骨i
-上瘾入骨i 2021-02-06 12:19

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

相关标签:
9条回答
  • 2021-02-06 12:41

    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.

    0 讨论(0)
  • 2021-02-06 12:43

    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."
    
    0 讨论(0)
  • 2021-02-06 12:43

    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"
    
    0 讨论(0)
  • 2021-02-06 12:44

    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"
    
    0 讨论(0)
  • 2021-02-06 12:49

    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."
    
    0 讨论(0)
  • 2021-02-06 12:52
    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.')
    
    0 讨论(0)
提交回复
热议问题