带时态的闰年标识符

2024-05-19 06:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个程序,用户输入他们想要的年份

然后程序用时态告诉你这一年是否是闰年

因此,如果这一年是2021年,它会说“2021年不会是闰年”

如果年份是2000年,它会说“2000年是闰年”

以下是我到目前为止所做的,但它似乎不起作用:

def is_leap_year(x):
    if x % 4 == 0:
        return True
    if x % 100 == 0:
        return False
    if x % 400 == 0:
        return True
    else:
        return False

def leap_year_answer(x):
    if x > 2020 and x % 100 == 0:
        print("Will not be a leap year")
    if x > 2020 and x % 400 == 0:
        print("Will be a leap year")
    if x < 2020 and x % 100 == 0:
        print("Was not a leap year")
    if x < 2020 and x % 400 == 0:
        print("Was a leap year")
    if x == 2020 and x % 400 == 0:
        print("Is a leap year")
    if x == 2020 and x % 100 == 0:
        print("Is not a leap year")

x = int(input("Enter your year: "))
print(leap_year_answer(x))

Tags: andanswer程序falsetruereturnifdef
3条回答

这就是我解决你问题的方法:

#!/usr/bin/python3
import datetime

def is_leapyear(year):
    if year % 400 == 0:
        return True
    if year % 100 == 0:
        return False
    if year % 4 == 0:
        return True
    else:
        return False

def print_leapyear(year):
    now = datetime.datetime.now()
    curr_year = now.year

    if is_leapyear(year):
        if  year >curr_year:
            print(str(year) + " will be a leapyear")
        elif year < curr_year:
            print(str(year) + " was a leapyear")
        else:
            print(str(year) + " is a leapyear")
    else:
        if year > curr_year:
            print(str(year) + " will not be a leapyear")
        elif year < curr_year:
            print(str(year) + " was no leapyear")
        else:
            print(str(year) + " is no leapyear")

year = int(input("Enter your year: "))
print_leapyear(year)

你关于如何确定闰年的计算是错误的。一年必须能被4整除,而不是被100整除,除非它能被400整除。 我使用了你必须计算闰年的函数,并在下面的函数中使用它,在这里我决定打印哪个时态。 然后,我使用datetime来确定当前日期,而不是硬编码当前日期,因此该脚本在2020年后也可以使用

首先,您的is_leap_year()函数将无法正常工作。例如,对于1900年,它将评估今年是闰年,因为它将第一个if语句评估为True。您的if子句必须嵌套以检查正确的内容(如果year可被4整除,请检查year是否也可被100整除。如果year可被100整除,请检查year是否可被400整除):

def is_leap_year(x):
    if x % 4 == 0:
        if x % 100 == 0:
            if x % 400 == 0:
                return True
            else: 
                return False
        else:
            return True
    else:
        return False

为了继续使用@Alfonso的建议(我也赞成),您可以使用f字符串创建一个更简单、更清晰的函数leap_year_answer,例如类似于以下内容:

def leap_year_answer(x):
    year = x
    verb_tense = "is" if year==2020 else ("was" if year<2020 else "will be")
    negation = "" if is_leap_year(year) else "not " 
    print(f"{year} {verb_tense} {negation}a leap year")

在代码中,似乎没有调用is_leap_year函数。然而,在闰年回答功能中,我认为在计算是否存在闰年时可能出现了错误。检查年份的计算不是闰年,我认为应该是:如果x%4=0另外,我认为检查它是否是闰年的计算应该是x%4==0

试试这个:

def leap_year_answer(x):
    if x > 2020 and x % 4 != 0:
        print("Will not be a leap year")
    if x > 2020 and x % 4 == 0:
        print("Will be a leap year")
    if x < 2020 and x % 4 != 0:
        print("Was not a leap year")
    if x < 2020 and x % 4 == 0:
        print("Was a leap year")
    if x == 2020 and x % 4 == 0:
        print("Is a leap year")
    if x == 2020 and x % 4 != 0:
        print("Is not a leap year")

x = int(input("Enter your year: "))
print(leap_year_answer(x))

相关问题 更多 >

    热门问题