How to exit a loop in Python? [closed]

情到浓时终转凉″ 提交于 2020-01-17 06:34:06

问题


I want to make the loop stops when x + y =z, on the else.

Code:

# -*- 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:


Replace exit with break. Exit isn't a way to exit loops in Python.

break statement docs



来源:https://stackoverflow.com/questions/33742912/how-to-exit-a-loop-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!