Gallery
Solution Tony's Grossery 👹
Share
Explore
Solution Tony's Grossery 👹

icon picker
Stage 1

1. understanding the code

run the program first
you know the program works so run it and see what happens!
you see a big long list of store items!
Juice - Happy Planet
Edible Flower - Mixed
Turkey - Mince
...
...
Carroway Seed
Pea - Snow
Now you need to look at the code to see how it is working and what the problem might be. It’s a lot of code!
Luckily the developers did leave some comments to help make sense of it. Well done guys, thanks!
Here are some snippets of the important sections.

snippet 1

# array of strings used to store list of products
productList = ['Juice - Happy Planet', 'Edible Flower - Mixed', 'Turkey - Mince', 'Spinach - Packaged',
...
...
'Pears - Bosc', 'Bacon Strip Precooked', 'Carroway Seed', 'Pea - Snow' ]
This are a lot of lines but they are very repetitive, lots of the same thing.
The comment tells you that this a string array and all the words are the products in the store. You can see each item may be several words in '' quote marks. The words are separated by , commas.

snippet 2

#prints out each product in the array
print(productList[0])
print(productList[1])
print(productList[2])
...
...
print(productList[148])
print(productList[149])
print('\n')
In this snippet the comment tells you that the code displays all the products from the array.
The first display statement is
print(productList[0])
and when you compare that to the output you see first in the list is:
Juice - Happy Planet
Now look at the set up for the array and you see it begins with
productList = ['Juice - Happy Planet',
Is that what you would expect? Can you explain what is going on here from the code?
Juice - Happy Planet is stored first position in the productList array
First position in the array is productList[0] since array positions start counting from 0.
So print(productList[0]) goes to the first position in the array and finds what was stored there; Juice - Happy Planet
The last display statement is
print(productList[149])
and when you compare that to the output you see last in the list is:
Pea - Snow
Now look at the set up for the array and you see it ends with
... , 'Pea - Snow' ]
You can that the long list of display statements are going through the array element positions from 0 to 149, one by one and outputting the product stored in the array element at that position. This is called traversing the array, like the mail delivery person walking along the street from the first house to last.
0-149, how many elements is that?
150, so there are 150 elements in the array, positions 0-149.
Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.