๐ ๏ธ Section project: a calculator
Section 2: Variables and inputLesson 2.4 ยท โฑ 10 min
๐ฏ Project goal
The program asks for two numbers, then prints the result of adding, taking away, multiplying and dividing.
a = int(input("First number: "))
b = int(input("Second number: "))
print("Sum =", a + b)
print("Difference =", a - b)
print("Product =", a * b)
print("Quotient =", a / b)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โFirst number: 12 Second number: 4 Sum = 16 Difference = 8 Product = 48 Quotient = 3.0
๐ Another example: the average calculator
Three marks, and we work out the average.
When the program asks, we type: 70, then 80, then 90.
a = int(input("Mark 1: "))
b = int(input("Mark 2: "))
c = int(input("Mark 3: "))
print("Average:", (a + b + c) / 3)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โMark 1: 70 Mark 2: 80 Mark 3: 90 Average: 80.0
โ๏ธ Practise
Challenge: add the remainder, and try dividing by zero. What happens?
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
A subtraction calculator: two numbers and the difference between them.
๐ Show the solution๐ Hide the solution
When the program asks, we type: 20, then 8.
a = int(input("First: "))
b = int(input("Second: "))
print(a, "-", b, "=", a - b)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โFirst: 20 Second: 8 20 - 8 = 12
Result: 20 - 8 = 12
Medium
A price calculator: the price of one item and the number, and the total.
๐ Show the solution๐ Hide the solution
When the program asks, we type: 15, then 5.
price = int(input("Price: "))
count = int(input("How many: "))
print("Total:", price * count)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โPrice: 15 How many: 5 Total: 75
Result: Total: 75
Hard
A full calculator: two numbers, and print adding, taking away, multiplying and dividing.
๐ Show the solution๐ Hide the solution
When the program asks, we type: 10, then 4.
a = int(input("a: "))
b = int(input("b: "))
print("+", a + b)
print("-", a - b)
print("*", a * b)
print("/", a / b)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โa: 10 b: 4 + 14 - 6 * 40 / 2.5
Result: adding 14, taking away 6, multiplying 40 and dividing 2.5

