简体   繁体   English

在python中定义一个包含匹配和大小写的函数

[英]defining a function that contains match and case in python

I want to define a function called sleep_in(weekday, vacation).我想定义一个名为 sleep_in(weekday, holiday) 的函数。 The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation.如果是工作日,则参数 weekday 为 True,如果我们正在休假,则参数 Vacation 为 True。 We sleep in if it is not a weekday or we're on vacation.如果不是工作日,或者我们正在度假,我们就睡在外面。

sleep_in(False, False) → True  
sleep_in(True, False) → False  
sleep_in(False, True) → True  
sleep_in(True, True) → True  

here's the function I defined这是我定义的函数

 def sleep_in(weekday, vacation):
   match (weekday, vacation):
    case (False, False):
     return True
    case (True, False):
     return False
    case (False, True):
     return True
    case (True, True):
     return True  

but I get the following error:但我收到以下错误:

invalid syntax (line 2)  

can anyone tell me what's wrong with my code?谁能告诉我我的代码有什么问题?
Edit:编辑:
Here's my full code in Jupiter Notebook!这是我在 Jupiter Notebook 中的完整代码!
Block of Code代码块

You have not followed the writing rules.你没有遵守写作规则。 You should leave a space the size of a tab.您应该留出一个制表符大小的空间。

def ...():
        match(...):
                case(...):
...

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

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