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

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

提交回复
热议问题