+ 1

Why they used 'counter' while defining ' word' ?

words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1 here why we need to use counter in 5th line. I mean why word = words [counter] why not word=workds only?

11th May 2018, 1:05 PM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
9 odpowiedzi
+ 1
words is an array of strings. "words[counter]" refers to the item in that array. It return Only one item, whereas calling words alone will return a dull array. Do you see the difference?
11th May 2018, 1:23 PM
Paul
+ 1
To be more specific: an array is a set of objexts which are numbered from 0 to n-1. let's say your array name is Arr. to "call" or " look at" the first item, you write Arr[0]. for the second: Arr[1] and so on. this ia why using a counter that goes from 0 to n-1 is so helpful and this is why you have to write it that way, to compare one string on the left side to the one on the other side.
11th May 2018, 1:26 PM
Paul
0
what does "{}" mean in Python?
11th May 2018, 1:25 PM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
0
may be I've got it 😅 Paul
11th May 2018, 1:27 PM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
0
Tanzim Ikram Sheikh - hope this helps! counter is used as index of list words: counter = 0 - > words[0] = 'hello' ... counter = 3 - > words[3] = 'eggs' After each iteration, counter needs to advance by 1 in order to print the next item in words: counter +=1 This is done, until the max index (3) is reached. Note that words has 4 items, so len(words) = 4, but list indices always start at 0. Hence: max_index = len(words) - 1
11th May 2018, 1:41 PM
Johannes
Johannes - avatar
0
I've finished basic lessons here but I am still not capable of writing codes myself. So if I go through all lessons will I be able to write codes on my own ? Jan Markus
11th May 2018, 2:50 PM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
0
Tanzim Ikram Sheikh - yes, you will be able to write simple programs in Python. I recommend you use a laptop or (pydroid if you're on a phone). The SL ide is quite limited.
11th May 2018, 2:59 PM
Johannes
Johannes - avatar
0
thanks a lot 😊Jan Markus
12th May 2018, 9:44 AM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
0
okay. btw thanks 😊Johannes
12th May 2018, 9:48 AM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar