Linked Questions

2 votes
7 answers
6k views

Python execute While loop even if condition is not met? [duplicate]

I am creating a car game. There are only "start", "stop", "quit" commands. Any other command is not recognized. command = "" while command != "quit": ...
toothpick's user avatar
  • 125
3 votes
4 answers
180 views

Why does this indentation work? python [duplicate]

here is my code: def is_prime(x): if x < 2: return False else: for i in range(2,x): if x % i == 0: return False else: ...
user3454635's user avatar
0 votes
1 answer
2k views

Python else statement inside and outside while loop [duplicate]

Given the following Python program, #Version 1 x = 15 y = 8 while x - y > 0: x -= 2 y += 1 print x, y if x % y == 0: break else: print x, y The output comes as: 13 9 11 ...
Dr. Debasish Jana's user avatar
-2 votes
2 answers
873 views

while else in python not executing the else [duplicate]

I have just started to explore python recently. So, I read about while else in python which sounds awesome but I am failing to execute a simple code, what am I am missing? env: python 3.8.5 (anaconda) ...
Genocide_Hoax's user avatar
0 votes
1 answer
298 views

"else" with "while" and mysterious "break" [duplicate]

I was going through this. Coming from C environment, it hit me right in the face with utter surprise and disbelief. And, then I tried it for myself:: bCondition = True while bCondition: print("...
Abhineet's user avatar
  • 5,429
0 votes
2 answers
140 views

Dark corners of python loops/keywords. Style advice [duplicate]

Learning python, found this code snippet. It reads text from stdin and prints "Done" when it finishes. I've never seen an else for a while loop anywhere else in any other language. The comments on the ...
user avatar
1 vote
1 answer
254 views

For else statement usage and meaning [duplicate]

I came across some code that has the structure: for val in list: do_something(val) if val is x: break else: do_something_else() I couldn't find much information about this structure ...
flau's user avatar
  • 1,398
-1 votes
1 answer
171 views

Python: purpose of "else" in while loop [duplicate]

My question is why there is such a thing as an "else" clause in a while loop. My code, for example, looks like this: a = 100 turns = 0 while a > 0: if func(a, b): #Function returns boolean ...
OscarVFE's user avatar
-2 votes
1 answer
47 views

Python. Searched for 2 days. Can't find answer. While loop for "response" for created game [duplicate]

Please help. I am so sorry but I cannot find an answer to make the loop stop after three questions. I've searched for two days. I am brand new at this and trying to teach myself. Everything runs ...
Brenda's user avatar
  • 1
764 votes
40 answers
832k views

How can I break out of multiple loops?

Given the following code (that doesn't work): while True: # Snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok.lower() == "y&...
Matthew Scharley's user avatar
795 votes
26 answers
359k views

Why does python use 'else' after for and while loops?

I understand how this construct works: for i in range(10): print(i) if i == 9: print("Too big - I'm giving up!") break else: print("Completed successfully&...
Kent Boogaart's user avatar
304 votes
22 answers
812k views

Is there a label/goto in Python?

Is there a goto or any equivalent in Python to be able to jump to a specific line of code?
user46646's user avatar
  • 160k
59 votes
5 answers
107k views

I'm getting an IndentationError (or a TabError). How do I fix it?

I have a Python script: if True: if False: print('foo') print('bar') However, when I attempt to run my script, Python raises an IndentationError: File "script.py", line 4 ...
Chris's user avatar
  • 23.2k
17 votes
4 answers
10k views

PyCharm warns local variable might be referenced

Why does PyCharm highlight the boolean variable nearby the return with Local variable "boolean" might be referenced before assignment? This code checks whether a number is prime or not: import ...
Mikeros's user avatar
  • 173
11 votes
5 answers
47k views

While else statement equivalent for Java?

What is the Java equivalent of the while/else in Python? Because it doesn't work in Java. The first chunk was my python code and the second portion is my attempt to translate it into Java. Edit: tring ...
MissSprezzatura's user avatar

15 30 50 per page