简体   繁体   English

赋值之前引用的局部变量,我不知道为什么会弹出

[英]local variable referenced before assignment, I can't figure out why this is popping out

These are the instructions: In the Gregorian calendar, three conditions are used to identify leap years:这些是说明: 在公历中,三个条件用于识别闰年:

The year can be evenly divided by 4, is a leap year, unless: The year can be evenly divided by 100, it is NOT a leap year, unless: The year is also evenly divisible by 400. Then it is a leap year.年份可以被 4 整除,是闰年,除非: 年份可以被 100 整除,它不是闰年,除非: 年份也可以被 400 整除。那么它就是闰年。

So I'm writing this code, which needs to be given a year and if the year is a multiple of 4 and 400 it is a leap year.所以我正在编写这段代码,它需要给出一年,如果年份是 4 和 400 的倍数,则它是闰年。 If it is a multiple of 100 it is not:如果它是 100 的倍数,则不是:

def is_leap(year):
    k = 400 % year
    m = 4 % year
    p = 100 % year
    if m == 0:
        if p == 0:
            if k == 0:
                leap = True
            else:
                leap = False
    return leap

year = int(input())
print(is_leap(year))

And this is the error message: UnboundLocalError: local variable 'leap' referenced before assignment这是错误消息: UnboundLocalError: local variable 'leap' referenced before assignment

I can't figure out why isn't working, leap isn't being used anywhere except for inside the loop我不知道为什么不工作,除了循环内部之外没有在任何地方使用跳跃

You just need to define leap so that it exists when the execution doesn't make it to your innermost block.您只需要定义跳跃,以便在执行未到达最里面的块时它存在。

def is_leap(year):
    k = 400 % year
    m = 4 % year
    p = 100 % year
    leap = False
    if m == 0:
        if p == 0:
            if k == 0:
                leap = True
    return leap

year = int(input())
print(is_leap(year))

Edit:编辑:

As others have correctly pointed out there are are some other problems with the code.正如其他人正确指出的那样,代码还存在其他一些问题。 In my experience date logic is much trickier than one would expect so instead of trying to roll your own I'd recommend using this:根据我的经验,日期逻辑比人们预期的要复杂得多,所以我建议不要尝试自己动手,而是使用这个:

import calendar
calendar.isleap(year)

Calendar documentation 日历文档

If m or p is not equal to 0 then you are returning variable that was not initialized.如果mp不等于 0,那么您将返回未初始化的变量。 You can set leap=False before first if , and completely delete else statement, and code will work as you probably want to.您可以在第一个if之前设置leap=False ,并完全删除else语句,代码将按照您可能想要的方式工作。

You should initialize leap variable before assigning it any value.您应该在分配任何值之前初始化leap变量。 That is what the problem is in my opinion.这就是我认为的问题所在。 This is how the code should be代码应该是这样的

def is_leap(year):
    leap = False
    k = 400 % year
    m = 4 % year
    p = 100 % year
    leap = False
    if m == 0:
        if p == 0:
            if k == 0:
                leap = True
            else:
                leap = False
    return leap

year = int(input())
print(is_leap(year))

There is many mistakes in your code你的代码有很多错误

  • 400 % year should be year % 400 , same for 4 and 100 400 % year应该是year % 400 ,对于4100相同
  • you may defined leap before the if , for the case you're not entering the if and so not defining it later您可以在if之前定义leap ,因为您没有输入if并且以后不定义它

More important your definition of leap year is wrong, your algorithm is wrong about it更重要的是你对闰年的定义是错误的,你的算法是错误的

def is_leap(year):
    m = year % 4
    p = year % 100
    k = year % 400
    leap = False
    if m == 0:  # should be multiple of 4
        leap = True
        if p == 0:  # exception if multiple of 100 : no leap
            leap = False
            if k == 0:  # exception if multiple of 400 : leap
                leap = True
    return leap

If you re-order the condition, you can avoid nesting them如果重新排序条件,则可以避免嵌套它们

def leapyr(n):
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    if n % 4 == 0:
        return True
    return False

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

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