Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带时态的闰年标识符_Python - Fatal编程技术网

Python 带时态的闰年标识符

Python 带时态的闰年标识符,python,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:

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

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

因此,如果这一年是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))
def是闰年(x):
如果x%4==0:
返回真值
如果x%100==0:
返回错误
如果x%400==0:
返回真值
其他:
返回错误
def闰年答案(x):
如果x>2020且x%100==0:
打印(“不会是闰年”)
如果x>2020且x%400==0:
打印(“将是闰年”)
如果x<2020且x%100==0:
印刷(“不是闰年”)
如果x<2020且x%400==0:
印刷(“是闰年”)
如果x==2020且x%400==0:
印刷(“是闰年”)
如果x==2020且x%100==0:
打印(“不是闰年”)
x=int(输入(“输入您的年份:”)
打印(闰年答案(x))
尝试使用
print(f“{year}{verb_tense}{negation}{a leap year”)
根据当前年份和用户给定年份(即您的x变量),解决所有这些变量(使用if子句)

下面您可以找到一些代码来定义这些变量:

if is_leapyear(year):
    negation = ""
else:
    negation = "not"
if year == current_year:  # get the current year on a previous step
    verb_tense = "is"
else:
    verb_tense = "was"
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))
def闰年回答(x):
如果x>2020且x%4!=0:
打印(“不会是闰年”)
如果x>2020且x%4==0:
打印(“将是闰年”)
如果x<2020且x%4!=0:
印刷(“不是闰年”)
如果x<2020且x%4==0:
印刷(“是闰年”)
如果x==2020且x%4==0:
印刷(“是闰年”)
如果x==2020且x%4!=0:
打印(“不是闰年”)
x=int(输入(“输入您的年份:”)
打印(闰年答案(x))

这是我解决您问题的方法:

#!/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)
#/usr/bin/python3
导入日期时间
def为年(年):
如果年份%400==0:
返回真值
如果年份%100==0:
返回错误
如果第%4年==0:
返回真值
其他:
返回错误
def打印年(年):
now=datetime.datetime.now()
当前年份=现在年份
如果是年(年):
如果年份>当前年份:
打印(str(年份)+“将是闰年”)
elif年份<当前年份:
打印(str(年)+“是跳跃年”)
其他:
打印(str(年份)+“是闰年”)
其他:
如果年份>当前年份:
打印(str(年份)+“不会是闰年”)
elif年份<当前年份:
打印(str(年份)+“不是闰年”)
其他:
打印(str(年份)+“不是闰年”)
年份=整数(输入(“输入您的年份:”)
打印年份(年)
你关于如何确定闰年的计算是错误的。一年必须能被4整除,而不是被100整除,除非它能被400整除。 我使用了你必须计算闰年的函数,并在下面的函数中使用它,在这里我决定打印哪个时态。
然后,我使用datetime来确定当前日期,而不是硬编码当前日期,因此该脚本在2020年后也可以使用

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

为了继续使用@Alfonso的建议(我也支持),您可以使用f字符串创建一个更简单、更清晰的函数
leap\u year\u 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")
def闰年回答(x):
年份=x

verb_tense=“is”if year==2020 else(“was”if year你能进一步解释吗?也许可以添加一个代码脚本来尝试?@AlfonsoSure!你想写(我将把变量放在方形刹车之间,用斜线分隔可能的值):“year[XXXX][is/was][not nia/]闰年”,因此基于我上面的代码:
print(f{year}{verb_tense}{negation}闰年“
为什么不在之前定义这个变量?(我在上面的答案中添加了代码)你能为我添加一个脚本,让我尝试使用固定版本吗“如果x%4!=0,或调用is_leap_year函数?@Aymaneljacifi“似乎不起作用”是什么意思?它是否运行?它运行但打印错误的结果?请给出输入和相应输出的示例。
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")