Plz explain this code to me I don't understand 👇 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Plz explain this code to me I don't understand 👇

https://code.sololearn.com/cVnJf6HJSDKe/?ref=app

29th Jul 2020, 7:03 AM
riya charles
riya charles - avatar
8 Answers
+ 6
This task can be done much better and easier to understand with a for loop. No counter, no max index needed: words= ["hello" , "world"] for word in words: print(word + '!',end=' ')
29th Jul 2020, 9:56 AM
Lothar
Lothar - avatar
+ 3
riya charles first a bit of confusion as your code title says for loop but the code inside is a while loop... https://code.sololearn.com/c2vJl2vPD5PB/?ref=app
29th Jul 2020, 7:23 AM
BroFar
BroFar - avatar
+ 2
words= ["hello" , "world"] counter=0 max_index= len(words)-1 #len(words) is 2, so max_index is 1 #FIRST RUN while counter<= max_index: #first run: 0<=1 word= words[counter] # word=words[0]="hello" print(word + "!") #print("hello!") counter= counter+1 #counter=0+1=1 #SECOND RUN while counter<= max_index: #second run: 1<=1 word= words[counter] # word=words[1]="world" print(word + "!") #print("world!") counter= counter+1 #counter=1+1=2 #THIRD RUN while counter<= max_index: #2<=1 is False, ending while loop
29th Jul 2020, 7:13 AM
Panko
Panko - avatar
+ 2
riya charles I just added the for loop in the code... in a while loop you are trying to do more functions at the same time like while I cook I am going to drink a soda... a for loop is more specific
29th Jul 2020, 7:33 AM
BroFar
BroFar - avatar
+ 2
Ok thanks...
29th Jul 2020, 7:36 AM
riya charles
riya charles - avatar
+ 2
Welcome
29th Jul 2020, 7:36 AM
BroFar
BroFar - avatar
+ 1
Thank u all. It a little hard to understand while loop 🙂🖐
29th Jul 2020, 7:26 AM
riya charles
riya charles - avatar
+ 1
This task can be done much better and easier to understand with a for loop. No counter, no max index needed: words= ["hello" , "world"] for word in words: print(word + '!',end=' ') Thank u for loop is simple I understood it.
29th Jul 2020, 11:56 AM
riya charles
riya charles - avatar