I can not understand this case | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can not understand this case

words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1

26th Jul 2019, 12:25 AM
Esraa Mohamed
1 Answer
+ 4
len() function returns the length of a list, in this case it returns 4 because it has Four strings separated by comma. Subtracting 1 from len function is 4 - 1 = 3 which will be stored in the max_index variable Now we will iterate through each object in the list until the condition is true. (0 <= 3) Now we are accessing the value at the index specified by the counter variable (words[counter]) from the list and assigning it to the variable word, which we used in printing On each iteration we are incrementing counter by 1 so that we don't get stuck in the loop forever.
26th Jul 2019, 1:53 AM
blACk sh4d0w
blACk sh4d0w - avatar