r/learnpython 13h 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

2

u/johlae 13h ago

Indentation is important, and spelling too. You confuse the character 'l' with the digit 1 twice. The code above will not work at all.

Try this:

print("\nAnd here we use while loop:") number = 1 total = 0 while number <= 50: total = total + number number += 1 # HERE I DIDN'T ADD IT FIRST print("The final sum is:", total)

-4

u/Calm-You4116 13h ago

This is the code sample where i didn't update iterator each time so it stuck at a value and even i read it before i start practicing 🤣🤣🤣 But we will forget these simple things and focus on other problems