VisuAlg Web

Forçando a Saída do Laço

Para interromper um laço imediatamente em Python, use break .

Para interromper um laço imediatamente em Python, use break.

Exemplo de códigoPython


for i in range(1, 100+1, 1):
print("Searching shelf ", i)
if i == 3:
print("Cookie found! Going to checkout.")
break  # Breaks the loop and jumps to the end of it

print("End of shopping.")
Exemplo de códigoPython


while True:
print("Enter even numbers (odd ends the system): ", end="")
num = int(input())

if num % 2 != 0:
print("Violation error! Stopping immediately.")
break

print("Accepted number: ", num)
if False:
break