简体   繁体   中英

CS50 Python - Ch 2 Conditionals

I am studying CS50 for Python right now. Towards the end of chapter 2 on Conditional is a 'match' function which is used to match names of students with their respective houses (in this example, Hogwarts to Griffyindor and Slytherin members). But I can't seem to execute the code on either Google Colab or CS50 IDE. I have also referred to Python Functional Documentation and it doesn't have anything on it.

Here's the code, can someone please tell me what am I doing wrong?

name = input("What's your name? ")

match name:
    case "Harry":
        print("Griffyindor")
    case "Hermoine":
        print("Griffyindor")
    case "Ron":
        print("Griffyindor")
    case "Malfoy":
        print("Slytherin")
    case_:
        print("Who?")

Error: line 5: match name:

File "", line 5 match name: ^ SyntaxError: invalid syntax

As mentioned by @molbdnilo, a space is missing between the "case" and the "_". This code should work...

name = input("What's your name? ")

match name:
    case "Harry":
        print("Griffyindor")
    case "Hermoine":
        print("Griffyindor")
    case "Ron":
        print("Griffyindor")
    case "Malfoy":
        print("Slytherin")
    case _:
        print("Who?")

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