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

后端 未结 9 1419
-上瘾入骨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.

提交回复
热议问题