break
Sun Jan 30 2022 12:35:53 GMT+0000 (Coordinated Universal Time)
Saved by
@marcpio
# Sometime you don't know when to exit end a loop in advance; in this case, use break statement to jump out of loop
# Ex: take input from a use until they type "done"
while True:
line = input('> ')
if line == 'done':
break
print(line)
# the loop condition is True, which is always true, so it runs until it hits the break statement
# if the use typed "done", the break statement exits the loop
# Use 'break' in loops that iteratively compute numerical values:
if abs(x - y) < epsilon: # epsilon is a very small number, e.g. 0.0000001
break
content_copyCOPY
Comments