如何在Python中退出循环?

时间:2015-11-16 19:18:54

标签: python syntax syntax-error

我希望在其他地方x + y =z上停止循环。

代码:

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 18:39:40 2015

@author: gabri
"""

from random import randint
import os

x = randint(1,11)
y = randint(1,11)

print ("", x,"+", y,"=\n")
z = int(input("Resposta="))

if z == x + y:
    input ("\n\nCorreto\nPrima enter para continuar...")

else:
    for z in range(0, 10):
        os.system('CLS')
        input ("Incorreto.\n\n Tente de novo...")
        x = randint(1,11)
        y = randint(1,11)
        os.system('CLS')
        print ("", x,"+", y,"=\n")
        z = int(input("Resposta="))
        if z == x + y:
            input ("\n\nCorreto\nPrima enter para continuar...")
            exit

1 个答案:

答案 0 :(得分:2)

exit替换为breakExit不是退出Python循环的方法。

break statement docs