๐ ๏ธ Section project: the money counter
Section 4: LoopsLesson 4.4 ยท โฑ 10 min
๐ฏ Project goal
The program asks for the price of 5 things, then prints the total.
total = 0
for i in range(5):
price = int(input("Price: "))
total = total + price
print("Total =", total)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โPrice: 10 Price: 25 Price: 5 Price: 40 Price: 20 Total = 100
๐ Another example: a money box with a goal
We add money until we reach 100.
When the program asks, we type: 40, then 30, then 50.
money = 0
while money < 100:
add = int(input("Add: "))
money = money + add
print("Now:", money)
print("Goal reached!")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โAdd: 40 Now: 40 Add: 30 Now: 70 Add: 50 Now: 120 Goal reached!
โ๏ธ Practise
Challenge: also print the highest price.
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
Save 5 pounds every day: how much after 30 days?
๐ Show the solution๐ Hide the solution
money = 0
for day in range(30):
money = money + 5
print(money)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โ150
Result: 150
Medium
How many days do you need to collect 200 pounds if you save 15 every day?
๐ Show the solution๐ Hide the solution
money = 0
days = 0
while money < 200:
money = money + 15
days = days + 1
print(days)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โ14
Result: 14 days
Hard
A coin counter: the number of 1, 5 and 10 coins, and print the total.
๐ Show the solution๐ Hide the solution
When the program asks, we type: 3, then 5, then 4.
ones = int(input("1s: "))
fives = int(input("5s: "))
tens = int(input("10s: "))
print("Total:", ones + fives * 5 + tens * 10)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โ1s: 3 5s: 5 10s: 4 Total: 68
Result: Total: 68

