下面是一个经过最小修改的代码版本,可以满足您的要求:
year = int(input("Enter the year to determine the number of days: "))
#month = int(input("Enter the month of the year: "))
#day = int(input("Enter the day of the year: "))
def if_leap_year(year):
if (year % 400 == 0): return 366
elif (year % 100 == 0): return 365
elif (year % 4 == 0): return 366
else:
return 365
#print(if_leap_year(year))
def days_in_month(month, year):
if month in {1, 3, 5, 7, 8, 10, 12}:
return 31
if month == 2:
if if_leap_year(year):
return 29
return 28
return 30
#print(days_in_month(month, year))
def days_left_in_year(month, day, year):
daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
return daysLeft
print(days_left_in_year(1,1,2020))
print(days_left_in_year(3,1,2020))
print(days_left_in_year(1,1,2022))
print(days_left_in_year(3,1,2022))
输出:
365
305
364
305
更新:
我想你只是想回答问题的第二部分:
def refund_period():
year = int(input("Enter the year to determine the number of days: "))
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
if if_leap_year(year) == 365:
money_owed = (days_left_in_year(month, day, year) / 365) * 278
if if_leap_year(year) == 366:
money_owed = (days_left_in_year(month, day, year) / 366) * 278
return money_owed
print(refund_period())
你的职能
refund_period()
没有归还任何东西,所以我补充说
return money_owed
,然后我加了一行
print(refund_period())
.
下面是一个运行示例:
Enter the year to determine the number of days: 2020
Enter the month of the year: 02
Enter the day of the year: 29
232.42622950819674