While loop list index out of range question

Hello everyone
I am stuck at the topic on while loop.
I wrote codes like below and it does not print whole index.
----code that I wrote—
python_topics = [“variables”, “control flow”, “loops”, “modules”, “classes”]
#Your code below:
length = len(python_topics)
index = 0
while index < length:
index += 1
print("I am learning about " + python_topics[index])

----result----
I am learning about control flow
I am learning about loops
I am learning about modules
I am learning about classes
Traceback (most recent call last):
File “script.py”, line 8, in
print("I am learning about " + python_topics[index])
IndexError: list index out of range

for the reference, I captured the coding that I wrote

I don’t know while “I am learning about variables” phrase is omitted here and list index out of range error comes up.
Please provide me with the kind answer.
Thank you in advance!

I’m not sure why a while loop is used here rather than a for loop b/c you already know the length of the list. Anyway…:slight_smile:

Did you happen to google that error by any chance?
Or, move the counter’s position in your code or maybe comment it out to see what happens (yikes, infinite loop!)?

A while loop will execute the code as long as the condition evaluates to True. The condition of the while loop will always be checked first and then the block of code will run. In this case,

while index < length:

The length of the items in your list is 5. anything beyond that does not evaluate to True. So, look at the placement of your counter…

2 Likes

When I moved the counter’s position. The problem was solved!
But still can’t figure out why the counter’s position could add the first index, "I am learning about variables”

What’s the index of the first item in the list?

You’ve defined the index and length.
The logic of the while loop will be executed as long as the condition evaluates to True.
Your condition is checked first, or, while index < length, then the code logic, or, your print() statement is executed, then the index is increased by 1 & added to the list.

What happens if you increase the value on the counter to +=2?
Mix it up with the code and see what happens until you understand what each component is doing. I think that helps one grasp concepts better.

1 Like

Thank you for your answer.
The first index there is “variables”.
the location of the counter’s position is important.
I presumably think the print("I am learning about " + python_topics[index]) itself is counted as the index 0.

Your starting index is 0.

switch up the values of the counter to see what gets printed. What is the output if you change it to +=5, etc?

It prints out “I am learning about variables”
even I set += 10, it still prints out “I am learning about variables”

Yes, b/c you’ve set the index at 0 and the while loop condition— while index < length:
(or, to write that out, “while the index—0–is less than the length…”
evaluates to True, thus the print() statement is executed.

2 Likes

Oh I understood!
Thank you for your kind explain!!!

2 Likes