How to finish executing the code in Python 3?

3
print('Calcula raizes equação 2º grau')
a = float(input('Informe o Valor de A'))
while a == 0:
    print('a equação não é do segundo grau')
    break
b = float(input('Informe o Valor de B'))
c = float(input('Informe o Valor de C'))
delta = (b ** 2) - 4 * (a * c)
print(delta)
while delta < 0:
    print('A equação não possui raizes reais')
    break
if delta > 0:
    print('possui 2 raizes reais ')
    raizdelta = delta ** 0.5
    print('Raiz de delta',raizdelta)
    x1 = (-b + raizdelta) / (2 * a)
    x2 = (-b - raizdelta) / (2 * a)
    print('x1',x1)
    print('x2',x2)

I'm having trouble when variable (a = 0) instead of finalizing code execution it continues ... what can I do?

    
asked by anonymous 16.09.2018 / 20:05

1 answer

3

If you want to terminate execution then you must use exit() and not break . It should actually be if and not while that does not make sense there. If I used inside a function (which I'd always prefer to do even to take the script face of the code) then I could only use a return to quit.

Unless you wanted to ask again, then while would be appropriate, but the logic would be different.

print('Calcula raizes equação 2º grau')
a = float(input('Informe o Valor de A'))
if a == 0:
    print('a equação não é do segundo grau')
    exit()
b = float(input('Informe o Valor de B'))
c = float(input('Informe o Valor de C'))
delta = (b ** 2) - 4 * (a * c)
print(delta)
if delta < 0:
    print('A equação não possui raizes reais')
    exit()
if delta > 0:
    print('possui 2 raizes reais ')
    raizdelta = delta ** 0.5
    print('Raiz de delta',raizdelta)
    x1 = (-b + raizdelta) / (2 * a)
    x2 = (-b - raizdelta) / (2 * a)
    print('x1',x1)
    print('x2',x2)

See running on ideone . And no Coding Ground . Also I put GitHub for future reference .

    
16.09.2018 / 20:33