๐ ๏ธ Section project: the shopping list
Section 5: ListsLesson 5.3 ยท โฑ 10 min
๐ฏ Project goal
The program collects 3 things from the user in a list, then prints them numbered.
items = []
for i in range(3):
items.append(input("Item: "))
for i in range(len(items)):
print(i + 1, "-", items[i])
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โItem: milk Item: bread Item: eggs 1 - milk 2 - bread 3 - eggs
๐ Another example: a list with prices
Two lists: the things and their prices, and the total at the end.
items = ["bread", "milk", "eggs"]
prices = [10, 25, 40]
total = 0
for i in range(len(items)):
print(items[i], prices[i])
total = total + prices[i]
print("Total:", total)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โbread 10 milk 25 eggs 40 Total: 75
โ๏ธ Practise
Try the lesson's examples here: copy the code, change it, and click โถ Run.
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
Add three things to an empty list and print it.
๐ Show the solution๐ Hide the solution
shop = []
shop.append("rice")
shop.append("tea")
shop.append("sugar")
print(shop)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โ['rice', 'tea', 'sugar']
Result: ['rice', 'tea', 'sugar']
Medium
Ask for 3 things with input, add them, and print the list.
๐ Show the solution๐ Hide the solution
When the program asks, we type: apples, then juice, then cheese.
shop = []
for i in range(3):
shop.append(input("Item: "))
print(shop)
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โItem: apples Item: juice Item: cheese ['apples', 'juice', 'cheese']
Result: the list with the three things.
Hard
Print the list numbered: 1. rice โฆ
๐ Show the solution๐ Hide the solution
shop = ["rice", "tea", "sugar"]
for i in range(len(shop)):
print(str(i + 1) + ".", shop[i])
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โ1. rice 2. tea 3. sugar
Result: 1. rice, 2. tea, 3. sugar

