Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

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 Answer

0 votes
by (40.4k points)

Try replacing exit with the break. Exit isn't a way to exit loops in Python.

For more information on break statement, refer to this docs:

https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...