+ 1
Code
words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1 Explain. using loop
1 Answer
+ 5
This here is a while loop. It will go round and round until its condition stops being True, then it ends.
This code introduces a list of words (four items) and the while loop iterates through it, assigning one item at a time to the word variable and printing word+'!'. Then it increments the counter.
The loop ends when the value of counter reaches the length of the list, as there are no more items to iterate on.