Beginner · 8 min
Calculate a total
Use numbers and variables to solve a small everyday problem.
Let Python do the arithmetic
Numbers do not need quotes. Use + to add, - to subtract, and * to multiply. Python calculates the expression before print() displays the result.
print(6 + 4)
print(9 - 2)
print(3 * 5)Output
10 7 15
Build a shopping total
Store the price and quantity as numbers, then multiply them. The same program can calculate another total when you change those values.
price = 4
quantity = 3
total = price * quantity
print(total)Output
12
Calculate a shopping total
- Identify the price and quantity.
- Multiply them and predict the displayed result.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Calculate a shopping total
Set price to 4 and quantity to 3. Store their product in total, then display total. Keep all three variables in your program.
Replace 0 with price * quantity. The * operator multiplies the two numbers.