定义一个包含匹配和情况的函数

发布于 2025-02-08 22:48:05 字数 740 浏览 0 评论 0原文

我想定义一个名为sleep_in的功能(工作日,假期)。如果是一个工作日,则工作日是正确的,如果我们要休假,则参数假期是正确的。如果不是一个工作日或我们正在度假,我们就会睡觉。

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

这是我定义的功能

 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  

,但我会收到以下错误:

invalid syntax (line 2)  

有人可以告诉我我的代码怎么了吗?
编辑:
这是我在木星笔记本中的完整代码!
代码块

I want to define a function called sleep_in(weekday, vacation). The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. 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!
Block of Code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静待花开 2025-02-15 22:48:05

您尚未遵守写作规则。您应该留下一个标签大小的空间。

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

You have not followed the writing rules. You should leave a space the size of a tab.

def ...():
        match(...):
                case(...):
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文