简体   繁体   English

CS50 Python - 第 2 章条件句

[英]CS50 Python - Ch 2 Conditionals

I am studying CS50 for Python right now.我现在正在研究 Python 的 CS50。 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).在第 2 章的结尾,有一个“匹配”函数,用于将学生的姓名与他们各自的学院(在本例中,霍格沃茨与格里芬多和斯莱特林成员)进行匹配。 But I can't seem to execute the code on either Google Colab or CS50 IDE.但我似乎无法在 Google Colab 或 CS50 IDE 上执行代码。 I have also referred to Python Functional Documentation and it doesn't have anything on it.我还提到了 Python 功能文档,它上面没有任何内容。

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:错误:第 5 行:匹配名称:

File "", line 5 match name: ^ SyntaxError: invalid syntax文件“”,第 5 行匹配名称:^ SyntaxError:无效语法

As mentioned by @molbdnilo, a space is missing between the "case" and the "_".正如@molbdnilo 所提到的,“case”和“_”之间缺少一个空格。 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?")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM