Python 匹配大小写:无论字母是否大写,我如何匹配大小写?

Python Match Case: How can I match a case no matter if the letters are capitalized or not easily?

提问人:boludoz 提问时间:11/9/2023 最后编辑:boludoz 更新时间:11/9/2023 访问量:77

问:

我有下一个代码(在 Python 中):

lang = input("What's the programming language you want to learn? ").upper()

match lang:
    case "JavaScript".upper():
        print("You can become a web developer.")

    case "Python".upper():
        print("You can become a Data Scientist")

    case "PHP".upper():
        print("You can become a backend developer")
    
    case "Solidity".upper():
        print("You can become a Blockchain developer")

    case "Java".upper():
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")

我只是在玩它,我发现它非常有用,但是当我运行它时,我收到以下错误:

[Running] python -u "...\match.py"
  File "...\match.py", line 4
    case "JavaScript".upper():
                     ^
SyntaxError: invalid syntax

这是一个错误,还是我如何以这种方式将条件转换为大写,而不必编写非常大的代码?

我阅读了文档,可以做很多事情,但我没有找到解决方案,或者我不知道如何查看它。

编辑:

根据 @Karl Knechtel 的意见,静态情况的可能解决方案如下:

lang = input("What's the programming language you want to learn? ").upper()

match lang:
    case "JAVASCRIPT":
        print("You can become a web developer.")

    case "PYTHON":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")

    case "SOLIDITY":
        print("You can become a Blockchain developer")

    case "JAVA":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")
python switch-statement 匹配

评论

1赞 Karl Knechtel 11/9/2023
“我怎样才能以这种方式将条件转换为大写”——为什么不直接用大写字母写它们,比如?“不必制作非常大的代码?”——我无法理解这种担忧。例如,如果你使用了明显的 / / 结构,就像我们在 Python 3.10 之前所做的那样,它应该是相同数量的代码行。case "JAVASCRIPT":ifelifelse
0赞 the23Effect 11/9/2023
@KarlKnechtel的观点是有道理的。但是为了学习,您是否检查了字符串和 upper() 函数之间是否有空格或“,”而不是“.”?
0赞 Amadan 11/9/2023
请注意,Python 不是 switch-statement,而是 pattern-matching。它们可能看起来很相似,并且确实重叠,但它们有不同的用途。至关重要的是,模式不是一个表达式,不能作为一个表达式来评估。正如 dupe 链接所说(以及 Karl Knechtel 的评论),Python 最接近语句的东西仍然是 .match... case...switchif... elif... else
1赞 blhsing 11/9/2023
@Amadan 尽管与远程相关,但此问题与您链接到的问题并不重复。这个问题是关于尝试计算语句子句中的表达式而导致的语法错误,该语句用于文本匹配或基于类型的捕获。投票重新开放。casematch
0赞 user19077881 11/9/2023
尽管正如 Knechtel 所指出的那样,没有真正的代码优势@Karl但您可以通过使用“guard”表达式来强制执行模式匹配: 等等。也就是说,除非您真的是模式匹配,否则链是首选。case x if x == "Javascript".upper():if-else

答:

0赞 sbottingota 11/9/2023 #1

问题是 python 无法像您尝试的那样计算大小写位中的表达式。你可以把它们都写成大写字母,从下面开始:

lang = input("What's the programming language you want to learn? ").upper()

match lang:
    case "JAVASCRIPT":
        print("You can become a web developer.")

    case "PYTHON":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")

    case "SOLIDITY":
        print("You can become a Blockchain developer")

    case "JAVA":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")

或者,你可以只使用 if 语句:

lang = input("What's the programming language you want to learn? ").upper()

if lang == "JavaScript".upper():
    print("You can become a web developer.")

elif lang == "Python".upper():
    print("You can become a Data Scientist")

elif lang == "PHP".upper():
    print("You can become a backend developer")
    
elif lang == "Solidity".upper():
    print("You can become a Blockchain developer")

elif lang == "Java".upper():
    print("You can become a mobile app developer")
else:
    print("The language doesn't matter, what matters is solving problems.")

注意您可能还希望删除 中的前导/尾随空格或标点符号。这是一种方法。lang

import string

lang = input("What's the programming language you want to learn? ")
lang = lang.upper().strip().strip(string.punctuation)