+ 3
Python - Loops?
I was taking the python course here and was confused about something ; loops. The example was this : words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1 I can't understand this, can someone simplify it for me?
1 Antwort
+ 5
Aries
words = list
max_index = length of words -1
max_index = 4 - 1
max_index = 3
Now,
While 0 < 3: means the loop will keep on running as long as this condition is true.
word = words[counter] means:
word = word[0]
word = "hello"
print ("hello" + "!")
counter = 0 + 1
counter = 1 now.
The loop will go to the beginning again.
word = words[counter] means:
word = word[1]
word = "world"...
it will get to a stage when counter will be three, then loop stops.
while 3 <= 3. loop stops here.
Basically all the word in words will be printed with the exclamation mark.
Hope this clarifies some things 😃😃