如何在Python中退出循环?

2022-02-26 00:00:00 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,"=
")
z = int(input("Resposta="))

if z == x + y:
    input ("

Correto
Prima enter para continuar...")

else:
    for z in range(0, 10):
        os.system('CLS')
        input ("Incorreto.

 Tente de novo...")
        x = randint(1,11)
        y = randint(1,11)
        os.system('CLS')
        print ("", x,"+", y,"=
")
        z = int(input("Resposta="))
        if z == x + y:
            input ("

Correto
Prima enter para continuar...")
            exit

解决方案

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

break statement docs

相关文章