0

I am combining a while-loop with an if-statement to add a number from 0-24 if the number is even. I constantly go to an infinite loop.

 number = range(0,50)
 i = 0
 total = 0

 while number[i]<25:
     if number[i]%2==0:
         total+=number[i]
         i+=1
     print(total)

1 Answer 1

1

The problem is, that you are adding 1 to i inside the if statement. You could simplify your code like this:

total = 0
for i in range(0, 25):
    if i % 2 == 0:
        total += i
print(total)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.