+ 1
Plz explain this code to me I don't understand đ
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=' ')
+ 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
+ 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
+ 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
+ 2
Ok thanks...
+ 2
Welcome
+ 1
Thank u all. It a little hard to understand while loop đđ
+ 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.