ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
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: 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:
Item: milk
Item: bread
Item: eggs
1 - milk
2 - bread
3 - eggs
๐Ÿ Open the code in Robo Python โ†—

๐Ÿ“Œ Another example: a list with prices

Two lists: the things and their prices, and the total at the end.

๐Ÿ A list with prices
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:
bread 10
milk 25
eggs 40
Total: 75
๐Ÿ Open the code in Robo Python โ†—
โœ๏ธ Practise
Try the lesson's examples here: copy the code, change it, and click โ–ถ Run.
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

Add three things to an empty list and print it.

๐Ÿ‘€ Show the solution
๐Ÿ Solution
shop = []
shop.append("rice")
shop.append("tea")
shop.append("sugar")
print(shop)
๐Ÿ–ฅ๏ธ The output in Robo Python:
['rice', 'tea', 'sugar']
๐Ÿ Open the code in Robo Python โ†—

Result: ['rice', 'tea', 'sugar']

Medium

Ask for 3 things with input, add them, and print the list.

๐Ÿ‘€ Show the solution

When the program asks, we type: apples, then juice, then cheese.

๐Ÿ Solution
shop = []
for i in range(3):
    shop.append(input("Item: "))
print(shop)
๐Ÿ–ฅ๏ธ The output in Robo Python:
Item: apples
Item: juice
Item: cheese
['apples', 'juice', 'cheese']
๐Ÿ Open the code in Robo Python โ†—

Result: the list with the three things.

Hard

Print the list numbered: 1. rice โ€ฆ

๐Ÿ‘€ Show the solution
๐Ÿ Solution
shop = ["rice", "tea", "sugar"]
for i in range(len(shop)):
    print(str(i + 1) + ".", shop[i])
๐Ÿ–ฅ๏ธ The output in Robo Python:
1. rice
2. tea
3. sugar
๐Ÿ Open the code in Robo Python โ†—

Result: 1. rice, 2. tea, 3. sugar

Finished the lesson? Mark it so your progress counts.