0
How To Use Break and While Function?
When i try to use while and break function in python.It show Error can you please tell me.How to use it?...
3 Respuestas
+ 1
Please save the code in Playground then share the link here so everyone can see and review it. Without a code to look at, it's rather difficult to find the problem and/or answer your question : )
+ 1
☝They are not functions, they are keywords!
while is a keyword used to create a while loop, that repeats it's code until it's condition gets False.
Example:
i = 0
while i < 5:
print(i)
i += 1
print("The loop is broken")
i = 0
This would start by testing i < 5.
Because i = 0 and 0 < 5 is True, the following 2 lines of code will be ran (once).
0 is printed and i changed it's value to 1.
The same i < 5 condition will now be tested again.
Because i = 1 and 1 < 5 is True, the 2 lines of code will be ran again.
1 is printed and i changed it's value to 2.
This carousel would be repeated until i = 5, which makes the i < 5 condition False, and the loop will be stopped.
"The loop is broken" would be printed.
i is changed back to 0, but loop will not continue, even though i < 5 would be true, that's because i < 5 wouldn't be tested anymore.
+ 1
break instead, it is a keyword used to create a statement to break out from the loop, even thought the condition were True.
Example:
while True:
print(1)
break
print(2)
print(3)
Output:
1
3