0

Can someone explain the first loop for me please?...i didn't understand the "counter" thing??

19th Jul 2018, 12:05 PM
Yahia
Yahia - avatar
31 Antworten
+ 2
One is old, one is new. There are many more differences though. You can search it up on the internet.
19th Jul 2018, 12:37 PM
Satyam
+ 2
code?
19th Jul 2018, 12:18 PM
Satyam
+ 2
the first loop of what?
19th Jul 2018, 12:19 PM
Satyam
+ 2
ok
19th Jul 2018, 12:19 PM
Satyam
+ 2
As long as the counter is less than or equal to the max index of the list, it prints the word with an exclamation mark.
19th Jul 2018, 12:22 PM
Satyam
+ 2
as list indices starts from 0, we do len(word) -1 to get the index of the last element.
19th Jul 2018, 12:23 PM
Satyam
+ 2
Why don't you try it?
19th Jul 2018, 12:24 PM
Satyam
+ 2
in counter = 1, it will start from the second element and so on.
19th Jul 2018, 12:24 PM
Satyam
+ 2
You can. It still works.
19th Jul 2018, 12:31 PM
Satyam
+ 1
the counter is used to iterate over the list words.
19th Jul 2018, 12:21 PM
Satyam
+ 1
and what if i write counter = 1
19th Jul 2018, 12:23 PM
Yahia
Yahia - avatar
+ 1
or counter = 2
19th Jul 2018, 12:23 PM
Yahia
Yahia - avatar
+ 1
notice we add 1 to the counter at the end of each loop.
19th Jul 2018, 12:25 PM
Satyam
+ 1
we add 1 so that the while loop doesn't keep going on for infinitely long.
19th Jul 2018, 12:26 PM
Satyam
+ 1
thank you i guess im starting to understand it a little
19th Jul 2018, 12:27 PM
Yahia
Yahia - avatar
+ 1
thank you for the explanation
19th Jul 2018, 12:29 PM
Yahia
Yahia - avatar
+ 1
Another thing: We can do it more easily using a for loop words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!") But I don't want to confuse you more!
19th Jul 2018, 12:29 PM
Satyam
+ 1
you're welcome!
19th Jul 2018, 12:30 PM
Satyam
+ 1
ok i guess i still have a long way to learn
19th Jul 2018, 12:32 PM
Yahia
Yahia - avatar
+ 1
I am talking about Python 3 BTW
19th Jul 2018, 12:33 PM
Satyam