简体   繁体   English

无法弄清楚为什么此代码未通过单个测试

[英]Cant figure out why this code is failing a single test

I'm trying to learn python more by solving the hacker rank puzzles and I can't understand why it is failing我正在尝试通过解决黑客等级难题来更多地学习 python,但我不明白为什么它会失败

This code is supposed to determine whether or not a year that is or above 1900 is, in fact, a leap year, the website says it passes 5 of 6 tests.该代码应该确定 1900 年或以上的年份是否实际上是闰年,该网站表示它通过了 6 次测试中的 5 次。 For the input 2100 it's returning true instead of false.对于输入 2100,它返回 true 而不是 false。

def is_leap(year):
    leap = False

    if year%4==0:
        return True
    elif year%100==0:
        return False
    elif year%400==0:
        return True


    return leap

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

I expect it to return false if the year is not divisible by 100 and return true if it is divisible by 4 or 400.如果年份不能被 100 整除,我希望它返回 false,如果它可以被 4 或 400 整除,则返回 true。

Let's see what happens if the year is, eg 1900, a year that was not a leap year:让我们看看如果年份是(例如 1900 年)不是闰年会发生什么:

Since year % 4 is 0 , we return True immediately, which is of course wrong.由于year % 40 ,我们立即返回True ,这当然是错误的。 In fact, any number that is divisible by 100 or 400 must also be divisible by 4, meaning that we never even get to the bottom two conditions.事实上,任何能被 100 或 400 整除的数字也必须能被 4 整除,这意味着我们甚至永远无法找到最底部的两个条件。

So the solution is to reorder your conditions from most to least specific:因此,解决方案是将您的条件从最具体到最不具体重新排序:

if year % 400 == 0:
    return True
elif year % 100 == 0:
    return False
elif year % 4 == 0:
    return True

Your second misunderstanding has to do with return : A return statement doesn't somehow "mark" a value to be the return value at the end of the function, it immediately exits the function, returning that value.您的第二个误解与return有关: return语句不会以某种方式将值“标记”为 function 末尾的返回值,它会立即退出 function,返回该值。 For better consistency, I would remove the leap variable, and just return False at the end of the function.为了获得更好的一致性,我将删除leap变量,并在 function 的末尾return False Alternatively, you could assign to leap inside the conditions instead of return ing there.或者,您可以指定在条件内leap而不是return那里。

Your if statement is not changing the variable leap .您的 if 语句不会更改变量leap

Try changing the variable leap like below.尝试如下更改变量leap

def is_leap(year):                                                                                                                                                          
    leap = False                                                                                                                                                            

    if year%4==0:                                                                                                                                                           
        leap = True                                                                                                                                                         
    elif year%100==0:                                                                                                                                                       
        leap = False                                                                                                                                                        
    elif year%400==0:                                                                                                                                                       
        leap = True                                                                                                                                                         


    return leap                                                                                                                                                             

year = int(input())                                                                                                                                                            
print(is_leap(year))   
def foo(year: int) -> bool:
    "find a year is leapyear or not"
    if year % 4 == 0 and year % 100 != 0:
        return True
    return year % 4 == 0 and year % 100 == 0 and year % 400 == 0

The function takes an integer as argument for the year parameter and returns True if the year is a leap year and False for the opposite. function 将 integer 作为 year 参数的参数,如果year是闰年则返回True ,反之则返回False A year needs to be divisible by 4 and not by 100 to be a confirmed leap year.一年需要能被 4 整除,而不是能被 100 整除才能确定闰年。 So, the conditional statement checks for that.因此,条件语句会对此进行检查。 If the condition's met it returns True else that function does not return anything and continues downward.如果满足条件,则返回 True 否则 function 不返回任何内容并继续向下。 Then, if the number is divisible by 4 and 100 and 400 as this case also means the year is a confirmed leap year, it returns True.然后,如果该数字可被 4 和 100 和 400 整除,因为这种情况也意味着年份是确认的闰年,则返回 True。 We have done all checks and still if that's not True then certainly the years not a leap year.我们已经完成了所有检查,如果那不是真的,那么这些年肯定不是闰年。

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

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