1

I've researched this problem but can't find a solution. As far as I know, my break statement is in my while loop, but I still get the Syntax error.

entered_number = 1

while entered_number >=0 :
    entered_number = int(input ("Number to add :"))
    sum += entered_number
    print("Entered number :",entered_number,"\nSum up til now:", sum)
else:
    print("The Final sum is :", sum)
    break

I know another solution to make my code work, but don't understand why this does not?

Thank you

1
  • 3
    Your 'break' statement is not inside the while loop. You can just remove it. When your program gets to the else, the loop is over. Commented Oct 16, 2021 at 17:09

3 Answers 3

2

Actually I figured out that the else statement does not need break to get out of the loop...

Sign up to request clarification or add additional context in comments.

1 Comment

else is not needed too. You can just print the final sum after the while loop.
0

Your break statement is not inside the loop. It’s in the scope of the else statement, but not in the scope of your while loop

Comments

0

A break statement must be inside a loop. In your example, it isn't, it's in the else block (the fact that the else is associated with a loop is inconsequential.

Then again, the else clause will only be executed once, so there isn't much of a point to having a break statement there anyway. Just remove it and you should be OK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.