Scenario: You are saving for a birthday gift. Write a program that plans your pocket money.
Your tasks
- Ask for the gift cost (use
floatso decimals are allowed). - Ask how much pocket money you get each week (also a
float). - Ask how much you spend each week (a
float). - Work out how much you can save each week: pocket money minus spending.
- Print how much you save each week.
- Work out how many whole weeks you need to save enough for the gift. Use integer division
//. If there is a remainder, you need one more week — use%to check. - Print the number of weeks needed.
💡 Remember:
input() always gives a string. Convert with float() before doing maths.💡 Try the whole challenge in your editor first. Only reveal the solution when you have had a proper go.
Solution
gift_cost = float(input("Gift cost: £"))
pocket_money = float(input("Pocket money each week: £"))
spending = float(input("Spending each week: £"))
# How much is left to save each week
saved_each_week = pocket_money - spending
print("You save £" + str(saved_each_week) + " each week")
# Whole weeks needed (round up if there is a remainder)
weeks = gift_cost // saved_each_week
if gift_cost % saved_each_week != 0:
weeks = weeks + 1
print("You need", int(weeks), "weeks to buy the gift")
Example output:
Gift cost: £25
Pocket money each week: £10
Spending each week: £3
You save £7.0 each week
You need 4 weeks to buy the gift
💡 With £7 saved each week, 25 ÷ 7 is 3 whole weeks and a remainder, so you need 4 weeks in total.