+ 2

What to do when it is saying outside the loop in python

6th Jun 2019, 7:47 AM
Anshuman Kapoor
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.
6th Jun 2019, 8:02 AM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 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.
6th Jun 2019, 8:17 AM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 5
Can you show the code you're getting this error in? Maybe I or someone else can help you if we have code.
6th Jun 2019, 7:51 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 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
6th Jun 2019, 8:10 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
⚒️Raj Chhatrala🛡️ You can use break outside of an if/else statement, but not outside of a loop 👆
6th Jun 2019, 8:16 AM
Anna
Anna - avatar
+ 3
n = 1 while n >= 8: print(n) n = n + 1 if n >= 5: print("Four") break
6th Jun 2019, 7:59 AM
Anshuman Kapoor
+ 2
Thank you so much kainatic i tried your code and it worked
6th Jun 2019, 8:12 AM
Anshuman Kapoor
+ 2
And thank you so much to Raj Chhatrala that you made my concept so much clear than before
6th Jun 2019, 8:13 AM
Anshuman Kapoor