0
I don’t understand this code. Why is it max_index = len(words)-1? And so on. Please help. Thank you.
words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1
3 ответов
+ 5
Because the first index starts at 0, so the last is len-1 => words[0] = 'hello' and words [ len-1] = 'eggs'.
0
words is a list, and in list u start counting the elements from 0
len(words) = length of words, = 4
max_index = len(words)-1, is saying max_index = 4-1 = 3
then it picks 0 and checks if it's less than 3, if it picks the element in the list which it's index is equal to 0, in this case that element is 'hello', it continues until it while loop stops.
I hope u understand, i'mnot good at explaining these kinda stuff, but i hope u get it
0
thank you guys for your help. I appreciate it a lot.