0

Please help me understand

I am trying to understand why this code does what it does, but I don't understand what the sections of code are doing. The 2 following lines from the code at the bottom are what I am confused about. word = words[counter] counter = counter + 1 words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1

18th Jun 2019, 6:32 PM
Joshua Edwards
3 odpowiedzi
0
The first line indicates the word to be accessed for display. The variable "counter" is the index. The second line increments the count. Basically, till the counter has reaches the last word, a word is accessed, displayed and the counter is incremented to access the next word. For example, "hello" is accessed first and displayed as the counter is at 0, then "world" is displayed as the counter is incremented by 1 and so on.
18th Jun 2019, 7:01 PM
Ashfiya Mumtaz Hassan
Ashfiya Mumtaz Hassan - avatar
0
Aaaaah.... I see. It was wierd seeing the counter in the square brackets and it threw me off
18th Jun 2019, 7:53 PM
Joshua Edwards
0
Thank you!
18th Jun 2019, 7:53 PM
Joshua Edwards