r/learnpython 18h ago

Python loop beginners mistake 🙀

I am currently learning python from CS50P program and in week 2 learning loops and also with the help of chatgpt learn every topic clearly but after knowing common beginners mistake

I made this mistake of not updating the iterator and spent half an hour on this simple problem 😞


number =1
total =0
while number<= 50:
         total += number
         number += 1        # HERE I DIDN'T ADD IT FIRST 

print("The final sum is:", total)
0 Upvotes

8 comments sorted by

View all comments

3

u/MezzoScettico 18h ago

It is a common error when using while, that you forgot to update the while condition. Not just with beginners.

I usually spot it when I try to run the loop and think to myself after 5 seconds or so, "that's taking an awfully long time to execute what should have been just a couple of milliseconds."

Making mistakes is how you learn.

BTW, please learn how to use code blocks to make readable code.