+ 2
What to do when it is saying outside the loop in python
8 Antworten
+ 11
Anshuman The while loop is never executed since the condition is false. This might make more sense:
n = 1
while n <= 5:
print(n)
n = n + 1
if n == 4:
print("Four")
break
The error you got might be due to the Indentation of "break" since break statement should be inside of any loop, not outside. Here's *your* code with proper indentation:
n = 1
while n >= 5: # Always false
print(i) # 'i' is not defined
n = n + 1
if n <= 9:
print("Four")
break
Edit: Glad I could help.
+ 9
You are correct Anna. I was just about to point that out and was writting it when you jumped the gun. That was exactly the concept I mentioned in my answer. ⚒️Raj Chhatrala🛡️ please fix your answer or Anshuman might live with false concepts.
+ 5
Can you show the code you're getting this error in?
Maybe I or someone else can help you if we have code.
+ 5
As kainatic said your loop will never run because of it's termination condition.
And the error is due to indentation of break statement.
So proper indentation will fix it
edit : check out answer by Anna
+ 4
⚒️Raj Chhatrala🛡️ You can use break outside of an if/else statement, but not outside of a loop 👆
+ 3
n = 1
while n >= 8:
print(n)
n = n + 1
if n >= 5:
print("Four")
break
+ 2
Thank you so much kainatic i tried your code and it worked
+ 2
And thank you so much to Raj Chhatrala that you made my concept so much clear than before