简体   繁体   English

为什么我的 function 没有给我预期的 output

[英]Why does my function does not give me the expected output

Given a year, determine whether it is a leap year.给定一个年份,判断它是否是闰年。 If it is a leap year, return the Boolean True, otherwise return False.如果是闰年,返回 Boolean True,否则返回 False。

Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.请注意,提供的代码存根从 STDIN 读取并将 arguments 传递给 is_leap function。只需完成 is_leap function。

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整除,则为闰年。

def is_leap(year):
    leap= False

    if (year % 400 == 0) and (year % 100 == 0):
        leap = True
    elif (year % 4 ==0) and (year % 100 != 0):
        leap=True
    else:
        pass

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

Input 2100 and expected output False输入 2100 和预期 output False

As @Grismar pointed your function is not returning anything so just add return leap to the end.正如@Grismar 指出的那样,您的 function 没有返回任何内容,因此只需将 return leap 添加到末尾即可。

def is_leap(year):
    leap= False

    if (year % 400 == 0) and (year % 100 == 0):
        leap = True
    elif (year % 4 ==0) and (year % 100 != 0):
        leap=True
    else:
        pass
    return leap

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

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

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