0

Please I don't understand how the codes on each item in a list is performed, can someone explain pls?

loops

12th Apr 2017, 3:11 PM
Akintaju Victoria
Akintaju Victoria - avatar
4 odpowiedzi
+ 4
Your code does not make a whole lot of sense, so I assume you meant words=["hello","world","spam","eggs"] counter=0 max_index=len(words)-1 while counter <=max_index: word=words [counter] print (word+"i") counter=counter+1 while checks if the condition is true, if yes it executes the indented block and then checks the condition again until the condition is not true anymore. In the first iteration counter is 0, then word is "hello" and "helloi" is printed. Then counter is 1, word is "world. In the fourth iteration counter will be 3, so the code prints "eggsi". And after that counter is increased to 4. The condition counter<=max_index is now false and the loop will not be executed another time.
12th Apr 2017, 3:42 PM
Tob
Tob - avatar
+ 4
please provide code.
12th Apr 2017, 3:15 PM
Meharban Singh
Meharban Singh - avatar
0
words=["hello","world","spam","eggs"] counter=0 max_index=len(words)-1 while counter <=max.index: word=words [counter] print (word+"i") counter=counter+1
12th Apr 2017, 3:22 PM
Akintaju Victoria
Akintaju Victoria - avatar
0
thanks tobi
12th Apr 2017, 3:48 PM
Akintaju Victoria
Akintaju Victoria - avatar