如何在 Python 中退出循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33742912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:52:46 来源:igfitidea点击:
How to exit a loop in Python?
提问by Gabriel Brandao
I want to make the loop stops when x + y =z
, on the else.
我想让循环在x + y =z
,时停止。
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
采纳答案by NotAnAmbiTurner
Replace exit
with break
. Exit
isn't a way to exit loops in Python.
替换exit
为break
。Exit
不是在 Python 中退出循环的方法。