为什么会出现此错误?

时间:2018-07-18 08:44:42

标签: python

我怀疑未检测到静态方法标签或什么东西。

>class Employee:
>   @staticmethod
>       def dayIsWorkday(day):
>           if day.weekday() == 5 or day.weekday() == 6:
>                 return False
>           return True
>
>
>import datetime
>my_date = datetime.date(2018, 12, 5)
>
>print(Employee.dayIsWorkday(my_date))
  

文件“ C:/Users/tronc/PycharmProjects/oop_TEST/main.py”,第26行      def dayIsWorkday(day):        ^   SyntaxError:语法无效

     

以退出代码1完成的过程

您可能会认为它没有用,我也认为这是有用的,但这是针对我正在尝试的教程,在我做错了事之前,我不想进一步尝试

2 个答案:

答案 0 :(得分:1)

@staticmethod之后的下一行中,函数名称不应缩进

>class Employee:
>   @staticmethod
>   def dayIsWorkday(day):
>       if day.weekday() == 5 or day.weekday() == 6:
>             return False
>       return True
>
>
>import datetime
>my_date = datetime.date(2018, 12, 5)
>
>print(Employee.dayIsWorkday(my_date))

答案 1 :(得分:1)

我猜它是缩进错误。 检查一下

class Employee:
    @staticmethod
    def dayIsWorkday(day):
        if day.weekday() == 5 or day.weekday() == 6:
            return False
        return True


import datetime
my_date = datetime.date(2018, 12, 5)

print(Employee.dayIsWorkday(my_date))