ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
Robo EgyptRobo Egypt
โ˜ฐ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

๐Ÿ› ๏ธ 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:
First number: 12
Second number: 4
Sum = 16
Difference = 8
Product = 48
Quotient = 3.0
๐Ÿ Open the code in Robo Python โ†—

๐Ÿ“Œ Another example: the average calculator

Three marks, and we work out the average.

When the program asks, we type: 70, then 80, then 90.

๐Ÿ The average calculator
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:
Mark 1: 70
Mark 2: 80
Mark 3: 90
Average: 80.0
๐Ÿ Open the code in Robo Python โ†—
โœ๏ธ Practise
Challenge: add the remainder, and try dividing by zero. What happens?
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

A subtraction calculator: two numbers and the difference between them.

๐Ÿ‘€ Show the solution

When the program asks, we type: 20, then 8.

๐Ÿ Solution
a = int(input("First: "))
b = int(input("Second: "))
print(a, "-", b, "=", a - b)
๐Ÿ–ฅ๏ธ The output in Robo Python:
First: 20
Second: 8
20 - 8 = 12
๐Ÿ Open the code in Robo Python โ†—

Result: 20 - 8 = 12

Medium

A price calculator: the price of one item and the number, and the total.

๐Ÿ‘€ Show the solution

When the program asks, we type: 15, then 5.

๐Ÿ Solution
price = int(input("Price: "))
count = int(input("How many: "))
print("Total:", price * count)
๐Ÿ–ฅ๏ธ The output in Robo Python:
Price: 15
How many: 5
Total: 75
๐Ÿ Open the code in Robo Python โ†—

Result: Total: 75

Hard

A full calculator: two numbers, and print adding, taking away, multiplying and dividing.

๐Ÿ‘€ Show the solution

When the program asks, we type: 10, then 4.

๐Ÿ Solution
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:
a: 10
b: 4
+ 14
- 6
* 40
/ 2.5
๐Ÿ Open the code in Robo Python โ†—

Result: adding 14, taking away 6, multiplying 40 and dividing 2.5

Finished the lesson? Mark it so your progress counts.