如何在Python中退出循环? [英] How to exit a loop in Python?

查看:42
本文介绍了如何在Python中退出循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使循环在其他时间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

这篇关于如何在Python中退出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆