- 1
why is there an endless while loop function in python?
For exemple: X = 0 While True: Print(x) X+=1 What can I do whit somthing like that? Sorry for bad english
4 Antworten
+ 3
while loops execute until the statement is true
and in your code true will be always true
so this can't be break
+ 2
usually in games thread cycles run infinitely.. and it will be destroyed only when game closes.. like capturing scores and sending it back to server and updating it.. it is a concept of multithreading.. which should run for infinite BUT it can also be destroyed by the user..
and u can see the text editors which keeps checking your spelling mistake and rendering it and parallel to it they stores the value to the document..
and another example is if you send a mail to someone but the email is not correct u get a no-reply mail that the recepient email is wrong. and if u have turned on auto reply..then automatically a mail will be send to the "no-reply" (which was a pregenerated mail) and it recieves your mail which it cannot understand again.. since the mail id is still incorrect.. so it will mail you back and the process continues..
these all examples are the way to implement endless loop... but at some extent they can be terminated
+ 1
Thank you for the reply
+ 1
If you want to break from infinite loop, you should use some breaking logic inside.
if X == 255:
break;
But it is redundant in that case, coz you can do that in the condition of the while loop:
while (X < 256):
print(X)
X = X + 1
You can use it with input that way:
wrong_inputs = 0
while True:
X = input()
# Enter 'break' or 'exit' to escape
if (X == "break" or X == "exit"):
break
wrong_inputs += 1
print(str(wrong_inputs) + " wrong inputs to " + str(X))