+ 1
Question about loop iteration (Python)
So the code goes like this... words=["Hello","world","spam","eggs"] a=0 b=len(words)-1 while a<=b : c=words[a] print(c+"!") a += 1 What I don't understand is the last line. As far as I can see, a is only assigned to the integer 0, so why when it is added on the last line, does it count the list number without referencing 'words' variable? If I remove the last line, the output only adds '!' To 'Hello'. Wouldn't a += 1 by itself, just count 0 + 1 indefinitely? I'm so confused wtf
7 Answers
+ 8
a is like an index variable. At first, a equals 0 so words[a] will return the first element of the list (list indices are zero-based). At the end of the loop, a is increased by 1, so now words[a] will return the second element of the list etc.
The loop will continue as long as a is <= len(words)-1 (length of the list minus 1, which is the index of the last element of the list).
> Wouldn't a += 1 by itself, just count 0 + 1 indefinitely?
No, because you still have the break condition while a <= len(words)-1. So it will only count up to the number of items in the list minus 1.
If you don't have a break condition, it will count indefinitely:
while True:
a += 1 # infinite loop, no break condition
But because you try to access the a.th element of the list, you will get an IndexError:
while True:
c = words[a]
a += 1
This loop doesn't have a break condition, but it will raise an error as soon as 'a' gets larger than len(words)-1. Because now you try to access the fifth element of a list that has only four elements.
+ 4
Anna oh!!!! is it because a+=1 changes the old value of a so c=words[a] becomes 1 and 2 and so on??
+ 4
No problem! We all have to start somewhere. And you basically found out the solution yourself, so I'm sure you're on a good way âșïž
+ 3
a is 0, so c = words[a] is the same as c = words[0], which is the first element of the list. Then a is increased and words[a] now is the same as words[1] which is the second element etc. The line c = words[a] is where the magic happens.
If you just look at the variable a, it will be 0 in the first iteration, 1 in the second iteration etc. c = words[a] is the "connection" between the index variable 'a' and the list 'words'. It will go from words[0] to words[1], words[2] all the way up to the last element of the list.
+ 3
Perryech Zx Exactly âșïžđ
+ 2
Anna thank you so much Anna!!!!! I have a long way to go đ youre the best
+ 1
Anna thank you!!! i understand slightly better now. however im still a little bit confused at one part...
words=["Hello","world","spam","eggs"]
a=0
b=len(words)-1
while a<=b :
c=words[a]
print (a)
this just prints 0 as a is still assigned to 0, but in the original code, a is somehow assigned to the first element of the list, but i see no such line. this is my understadning of all the lines:
c=words[a] this assigns the first element "Hello" to variable c
print(c+"!") so this adds '!' to 'Hello' until the loop breaks
a += 1 this is where i am confused. isnt a at this point only integer 0 and not element 0
apologies because i am still really really new, but i just cannot get my mind over this!!!