如何在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
替换为break
。Exit
不是在Python中退出循环的方法。
break statement docs
相关文章