Sorted for Bhaskara formula

Asked

Viewed 166 times

1

I have an exercise to solve and I have a problem, it is not to be done in list or dictionary. If you have 2 real roots they should be printed in ascending order, the problem is that you should print the following "the roots of the equation are X and Y". I am unable to assemble the print with both results in ascending order.

I made the code to follow:

import math

a = float(input("Digite o valor de a: "))

b = float(input("Digite o valor de b: "))

c = float(input("Digite o valor de c: "))

delta = b ** 2 - 4 * a * c

if delta == 0:
    raiz1 = (-b + math.sqrt(delta)) / (2 * a)
    print(f"a raiz desta equação é {raiz1}")
else:
    if delta < 0:
        print("esta equação não possui raízes reais")
    else:
        raiz1 = (-b + math.sqrt(delta)) / (2 * a)
        raiz2 = (-b - math.sqrt(delta)) / (2 * a)
        result = raiz1, raiz2
        print("As raízes da equação são", sorted(result))

EDIT: Solved without using Sort

I did two checks and I managed to solve this way:

if raiz1 < raiz2:
    print(f"as raízes da equação são {raiz1} e {raiz2}")
elif raiz2 < raiz1:
    print(f"as raízes da equação são {raiz2} e {raiz1}")

1 answer

0


You don’t need to put Elif if you’re no longer in a position to consider.

if raiz1 < raiz2:
    print(f'as raízes da equação são {raiz1} e {raiz2}')
else:
    print(f'as raízes da equação são {raiz2} e {raiz1}')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.