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

后端 未结 9 1423
-上瘾入骨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:57
    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.")
    
    0 讨论(0)
  • 2021-02-06 12:57

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

    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..."
    
    0 讨论(0)
提交回复
热议问题