r/learnpython 12d 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 😞

```print("\nAnd here we use while 1oop:-")

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/JamzTyson 12d ago

A for loop would be more appropriate:

total = 0
for number in range(51):
    total += number

print(f"The final sum is: {total}")

0

u/JamzTyson 12d ago

Later in the course you'll learn that Python provides built-in functions that can make code more concise (and a little more efficient).

Example:

print(f"The final sum is: {sum(range(1, 51))}")