[英]Leap year identifier with tense
我正在嘗試制作一個用戶輸入他們想要的年份的程序。
然后程序會用時態告訴你這一年是否是閏年。
所以,如果這一年是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))
這是我解決您問題的方法:
#!/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 年之后可用。
嘗試使用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 函數。 但是,在leap_year_answer 函數中,我認為您計算是否存在閏年可能存在錯誤。 檢查年份的計算不是閏年,我認為應該是:如果 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))
首先,您的is_leap_year()
函數將無法正常工作。 例如,對於 1900 年,它將評估今年是閏年,因為它將第一個if
語句評估為True
。 您的if
子句必須嵌套以檢查正確的事情(如果 year 可被 4 整除,請檢查它是否也可被 100 整除。如果它可被 100 整除,還要檢查它是否可被 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.