Reputation: 9
I'm new to python, and I'm trying to make a pocket money/ saving calculator, where it assumes you get money every week and a set amount of money on birthdays/christmas and tell you how long it will take to reach the amount you are trying to save up for. It works well in counting the weeks it would take to reach the goal, but it seems to ignore the special dates. Is there a way I can change the code so that it would take them into account?
import datetime as dt
import math as mt
current_balance = float(input("What is your current balance?: £"))
pocket_money = float(input("How much pocket money do you get per week?: £"))
goal = float(input("How much money do you need?: £"))
today = dt.date.today()
if goal <= current_balance:
print(f"You already have enough money and you will have £{current_balance-goal} left.")
else:
money_needed = goal - current_balance
datetest = today
d = dt.timedelta(days=1)
counter = 0
birthday = dt.datetime(datetest.year, 5, 16)
christmas = dt.datetime(datetest.year, 12, 25)
while money_needed > 0:
if datetest.weekday() == 0:
money_needed = money_needed - 5
counter += 1
datetest += d
elif datetest == birthday or datetest == christmas: #<-- This bit seems to do nothing
money_needed = money_needed - 200
if datetest.weekday() == 0:
counter += 1
datetest += d
else:
datetest += d
print(counter)
e.g. if the first three inputs were 10, 5, and 500, it would return 98, which is the number of weeks it would take if the birthday/christmas was ignored.
Upvotes: 0
Views: 61
Reputation: 11912
A few issues:
pocket_money
at all in your script, you're just using money_needed = money_needed - 5
each week, so the calculation is messed up even before trying to consider christmas and birthdays.datetest += d
runs inside every possible date, so no need to repeat that 3 times in the loopcounter += 1
as it always happens on the same weekday regardless of holidaysLastly, the most important issue: If you print out today
you will see it's equal to 2021-09-06
but if you print out your birthday
for example, you will see it is equal to 2021-05-16 00:00:00
. Notice a different in their format? They can never be equal! One is a datetime
object and the other just a date
object. To fix that, compare the date directly (day and month) or just use date
instead of datetime
when defining them
To implement all of that:
import datetime as dt
import math as mt
current_balance = float(input("What is your current balance?: £"))
pocket_money = float(input("How much pocket money do you get per week?: £"))
goal = float(input("How much money do you need?: £"))
today = dt.date.today()
if goal <= current_balance:
print(f"You already have enough money and you will have £{current_balance-goal} left.")
else:
money_needed = goal - current_balance
datetest = today
d = dt.timedelta(days=1)
counter = 0
birthday = dt.date(datetest.year, 5, 16)
christmas = dt.date(datetest.year, 12, 25)
while money_needed > 0:
datetest += d
if datetest.weekday() == 0:
money_needed = money_needed - pocket_money
counter += 1
if datetest == birthday or datetest == christmas:
money_needed = money_needed - 200
print(counter)
Upvotes: 1