Python Arena
Step 5 of 5

Calculating with variables

A program that works with numbers usually reads them, stores them in variables and then calculates with the variables. This one adds up three days of running, in whole kilometers, and works out the distance per day:

monday = int(input("Monday, km: "))
tuesday = int(input("Tuesday, km: "))
wednesday = int(input("Wednesday, km: "))
total = monday + tuesday + wednesday
print(f"Distance run: {total} km")
print(f"Per day: {total / 3} km")
Input
5
8
2
Output
Monday, km: 5
Tuesday, km: 8
Wednesday, km: 2
Distance run: 15 km
Per day: 5.0 km

Adding to a variable

Another way is to keep a running total in one variable and add each new value to it as it comes in:

total = 0
price = float(input("First item: "))
total = total + price
price = float(input("Second item: "))
total = total + price
print(f"To pay: {total}")
Input
3.5
2.25
Output
First item: 3.5
Second item: 2.25
To pay: 5.75

As before, total = total + price works out the right side first with the current values, then stores the result back in total. The total starts at 0, so the first addition has something to add to. Here one variable, price, holds each item in turn: after it's added to the total, its old value isn't needed any more.

The shorthand +=

Increasing a variable is so common that Python has a shorthand: total += price means the same as total = total + price. There are also -= and *=, and they work the same way:

balance = 100
balance += 25
balance -= 40
balance *= 2
print(balance)
Output
170

100 plus 25 is 125, minus 40 is 85, and doubled that's 170.

Keep the values you need

Reusing one variable is fine for a running total. But if the program needs every value at the end, each one needs a variable of its own:

morning = int(input("Temperature in the morning: "))
evening = int(input("Temperature in the evening: "))
print(f"From {morning} to {evening} degrees: a change of {evening - morning}")
Input
12
19
Output
Temperature in the morning: 12
Temperature in the evening: 19
From 12 to 19 degrees: a change of 7

When you do reuse a variable, keep it for values that play the same part, like the prices above. A variable value that first holds the number of guests and later the price of a ticket would work, but the reader would have to work out what it means on every line. guests and ticket_price say it at once.

Your task

Challenge 1 of 3

Fill in the blanks so the program adds both answers to total. If the user types 3 and 5, the run looks like this:

Apples: 3
Pears: 5
Fruits: 8

Challenge 2

Write a program that asks for three test scores (whole numbers) and prints their total and their mean. If the user types 7, 9 and 8, the run looks like this:

Score 1: 7
Score 2: 9
Score 3: 8
Total: 24
Mean: 8.0

Challenge 3

Write a program for a cash register. It asks for the prices of three items, which may have decimals, and prints the running total after each one. Keep the total in one variable and add each price to it with +=. If the user types 2.5, 4 and 1.25, the run looks like this:

Item 1: 2.5
Total so far: 2.5
Item 2: 4
Total so far: 6.5
Item 3: 1.25
Total so far: 7.75