我沒有使用任何日期時間模塊。我創建了自己的函式來計算日、月和年。我想根據日期計算退款。如果日期無效,它應該要求用戶重試,直到日期為真。
year = 0
month = 0
day = 0
money_owed = 0
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 is_valid_date(year, month, day):
if days_in_month(month, year)<day:#checks if the given day is possible
given the month
return False
else:
return True
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
def refund_period():
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
year = int(input("Enter the year to determine the number of days: "))
if is_valid_date(year , month , day):
money_owed = (days_left_in_year(month, day, year) /
if_leap_year(year)) * 278
return round(money_owed, 2)
else:
print("Your date is invalid, try again.")
while is_valid_date(year, month, day):
print('you will be refunded','$', refund_period())
break
else:
print("Your date is invalid, try again.")
我正進入(狀態:
you will be refunded $ -8.38
即使由于日期無效而不應執行計算
uj5u.com熱心網友回復:
您正在設定year =0 , month =0, day = 0第一個回圈。也 while不清楚。您的所有函式都回傳一個 int,因此永遠不要驗證日期是否正確。也許您可以創建一個函式來驗證日期,如下所示:
def is_valid_date(year , month , day):
if month <1 or month >12: #Validate a allowed month
return False
if day <1 or day > days_in_month(month, year): # validate an allowed day for the month
return False
return True
您可以更改此功能:
def refund_period():
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
year = int(input("Enter the year to determine the number of days: "))
if is_valid_date(year , month , day):
money_owed = (days_left_in_year(month, day, year) / if_leap_year(year)) * 278
return round(money_owed, 2)
else :
print("Your date is invalid, try again.")
只是一些評論:
您正在使用年、月和日,
input()因此您無需為此創建全域變數。你不需要問,
if_leap_year(year) == 365 or 366因為這個函式回傳365還是366,所以你可以像我一樣在計算money_owned時直接使用它。您也可以
if_leap_year(year)改用(if_leap_year(year) if month < 3 else 365). 該函式回傳 366 或 365,您無需再次驗證。您可以對函式
daysInMonth內部的變數使用串列推導days_left_in_year:daysInMonth = [days_in_month(m, year) for m in range(1,13)]
uj5u.com熱心網友回復:
您的 while 回圈不是比較函式值,而是檢查物件是否存在。而不是像這樣的條件while days_left_in_year(month, day, year),使用像這樣的條件while days_left_in_year(month, day, year)<30(假設您想拒絕超過 30 天的訂單退款。
要驗證日期,請在您的評論下添加以下函式#print(days_in_month(month, year)):
def is_valid_date(year, month, day)
if days_in_month(month, year)<day:#checks if the given day is possible given the month
return False
else:
return True
那么你的情況應該是這樣的:
if is_valid_date(year, month, day) == True:
print('you will be refunded','$', refund_period())
else:
print("Your date is invalid, try again.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454529.html
