如何制作一个循环,根据创建的函数验证用户输入的日期?

How to make a loop that validates the user-input dates based on the functions created?

我没有使用任何日期时间模块。我创建了自己的函数来计算日、月和年。我想根据日期计算退款。如果日期无效,它应该要求用户重试,直到日期为真。

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

尽管不应执行计算,因为日期无效

你的 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) and (month<13)):
    print('you will be refunded','$', + refund_period())
else:
    print("Your date is invalid, try again.")

您正在第一个循环中设置 year =0 , month =0, day = 0while 也不清楚。你所有的功能 return 一个 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因为这个函数return是365还是366 所以你在计算money_owned的时候可以直接用, 和我一样。

  • 也可以用if_leap_year(year) 代替 (if_leap_year(year) if month < 3 else 365)。功能return366或365,无需再次验证

  • 并且您可以在 days_left_in_year 函数中为您使用列表理解 daysInMonth 变量: daysInMonth = [days_in_month(m, year) for m in range(1,13)]