Codiqo

Learn Python · lesson 8 of 60 · about 8 minutes

Project: Savings Planner

Put input, conversion and f-strings together in a planner that works out what's left to save, shown neatly in pounds and pence.

What you'll learn

  • Plan a small program step by step
  • Read and convert several inputs
  • Format money with :.2f in f-strings

A savings planner

You want to know how long until you can buy something. The planner asks three questions (the price, what you've saved, and how much you put aside each week), then works out what's left and how many weeks that takes. Plan it before you write it:

1. Ask the three questions
2. Convert each answer with float(), because prices can have pence
3. Work out what's left, and the weeks
4. Print it neatly

Step through the sums with fixed numbers first

price = 120.0
saved = 45.0
weekly = 15.0
left = price - saved
weeks = left / weekly
print(f"Still to save: {left}")
print(f"Weeks to go: {weeks}")

Output

Still to save: 75.0
Weeks to go: 5.0

Money with two decimal places

75.0 isn't how money looks. Inside the braces, a colon adds a format: {left:.2f} shows exactly two decimal places, rounding if needed, and {weeks:.1f} shows one.

Formats inside the braces

left = 75
print(f"Still to save: {left:.2f}")
print(f"{19.999:.2f}")

Output

Still to save: 75.00
20.00

The whole planner

price = float(input("Price? "))
saved = float(input("Saved? "))
weekly = float(input("Each week? "))
left = price - saved
print(f"Still to save: {left:.2f}")
print(f"Weeks to go: {left / weekly:.1f}")

Output

Price? Saved? Each week? Still to save: 75.00
Weeks to go: 5.0

Tip

float() reads 120 as well as 19.99, so it's the safe choice for money someone types in.

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.