Adding a bonus for specific days in Python

Question:

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)

Asked By: jonahpocalypse

||

Answers:

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
Answered By: KianFakheriAghdam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.