Home > front end >  Adding a bonus for specific days in Python
Adding a bonus for specific days in Python

Time:10-30

I'm trying to add a bonus to my code that is for Dec 15 - 22 at a rate of $50 per day if those days are used. I can't really seem to figure out how to do that currently.

The input for this value is based off a start date and an end date that are input by the user and are in YYYY/MM/DD format:

while True:

    EndDateStr = input("Enter the end date of the trip (YYYY-MM-DD): ")
    if EndDateStr == "":
        print("End date cannot be blank. Please re-enter.")
        continue

    try:
        EndDate = datetime.datetime.strptime(EndDateStr, "%Y-%m-%d")
    except:
        print("Date must be in YYYY-MM-DD format. Please re-enter.")
        continue
    break

while True:
    DayBonus = 0
    NumDays = int(input("Enter the number of days: "))

    if NumDays == "":
        print("The number of days cannot be blank. Please re-enter.")
    elif NumDays > 7:
        print("The number of days cannot be 7 days past the start date. Please re-enter.")
    elif NumDays >= 4:
        DayBonus = BONUS_ABOVE_3
        break

Thanks in advance. (The DayBonus is not the bonus I am referring to - that's another bonus I'm not having issues with)

CodePudding user response:

I would recommend checking the timedelta part of datetime module. We can subtract NumDays from EndDate.

StartDate = EndDate - datetime.timedelta(days=NumDays)

and using this information as you already know, we can check for the following.

bonus() if StartDate.month == 12 and StarDate.day in range(15, 23) else 0
  • Related