To resolve this issue you should pay attention to some things, such as:
- Knowing how to capture the values of coefficients;
- Calculate the value of delta and, also, the roots;
- Correctly display function return - according to your constraints;
- Implement a noose to re-enter the code, if the user wishes.
Putting this logic into practice, I implemented the following code:
def raizes(a, b, c):
d = ((b ** 2) - (4 * a * c))
if d < 0:
msg = 'Não existe raízes Reais!'
return msg
else:
x1 = round(((-b + (d ** (1 / 2))) / (2 * a)), 1)
x2 = round(((-b - (d ** (1 / 2))) / (2 * a)), 1)
if x1 == -0.0:
x1 = 0
if x2 == -0.0:
x2 = 0
menor = x1
if x2 < x1:
maior = x1
menor = x2
else:
maior = x2
if d == 0:
msg = f'Duas raízes Reais iguais: x1 = {menor} e x2 = {maior}'
return msg
else:
msg = f'Duas raízes Reais diferentes: x1 = {menor} e x2 = {maior}'
return msg
while True:
coef_A, coef_B, coef_C = list(map(float, input('Coeficientes "A", "B" e "C": ').split()))
print(raizes(coef_A, coef_B, coef_C))
resp = input('Desejas realizar novo cálculo? [S/N]: ').upper()
while (len(resp) != 1) or (resp not in 'SN'):
print('Valor INVÁLIDO! Digite apenas "S" ou "N"!')
resp = input('Desejas realizar novo cálculo? [S/N]: ').upper()
if resp == 'N':
break
Note that when we execute this code we receive the following message: Coeficientes "A", "B" e "C":
. At this point we must enter the 3 coefficients of that equation, in same line, separated for a single space and press enter
. At this time the entered values are captured and sent as parameters to the function roots(a, b, c). Arriving there will be calculated the delta value - d. Once the result of the delta is reached, it shall be verified whether this value is less than 0. If this value is in fact less than 0 the return of the function will be: "Não existe raízes reais"
. If this value is different from 0 the code flow will be diverted to the block Else.
Once there, the roots of the quadratic function will be calculated.
OBSERVING:
According to the Fundamental Theorem of Algebra,"Every nonconstant polynomial of degree n has n complex roots, not necessarily all distinct ones".
In short: any quadratic function will always have 2 roots, may be: 2 equal REAL roots or 2 different REAL roots or 2 different IMAGINARY roots
Well, at this point it will be checked whether the delta value - d - is equal to 0, or rather, if d == 0. If so, the quadratic function will have two equal real roots, i.e. X1 == x2. Otherwise, the quadratic function will have two different real roots, i.e., X1 != x2
After you have displayed the result of the calculations we receive the following message: Desejas realizar novo cálculo? [S/N]:
. Right now we must type S, to perform recalculation or N to end the execution of the code.
Let’s test the code?
Example 1:
Calculate the roots of the quadratic function x² - 3x + 2.
At this time we execute the code and when we receive the message: Coeficientes "A", "B" e "C":
. We should type...
1 -3 2
...and press enter
.
At this point the code will perform the calculations and provide us with the following output:
Duas raízes Reais diferentes: x1 = 1.0 e x2 = 2.0
Desejas realizar novo cálculo? [S/N]:
If we want to perform a new calculation just press the letter S. Otherwise, we should press the letter N.
Example 2
Calculate the roots of the quadratic function -3x² + 6.
Observing: In this role we do not have the coefficient of B. In this case we will type 0 for the value of the coefficient of "B".
Then we execute the code and when we receive the message: Coeficientes "A", "B" e "C":
. We should type...
-3 0 6
...and press enter
.
At this point the code will perform the calculations and provide us with the following output:
Duas raízes Reais diferentes: x1 = -1.4 e x2 = 1.4
Desejas realizar novo cálculo? [S/N]:
Example 3
Calculate the roots of the quadratic function x - 2x².
Observing: In this function we have the order of the terms exchanged. In this question the A == -2, the B == 1 and C == 0.
Then we execute the code and when we receive the message: Coeficientes "A", "B" e "C":
. We should type...
-2 1 0
...and press enter
.
At this point the code will perform the calculations and provide us with the following output:
Duas raízes Reais diferentes: x1 = 0 e x2 = 0.5
Desejas realizar novo cálculo? [S/N]:
Example 4
Calculate the real roots of the quadratic function x² + 4x + 5.
Then we execute the code and when we receive the message: Coeficientes "A", "B" e "C":
. We should type...
1 4 5
...and press enter
.
At this point the code will perform the calculations and provide us with the following output:
Não existe raízes Reais!
Desejas realizar novo cálculo? [S/N]:
Example 5
Calculate the real roots of the quadratic function x² + 4x + 4.
Then we execute the code and when we receive the message: Coeficientes "A", "B" e "C":
. We should type...
1 4 4
...and press enter
.
At this point the code will perform the calculations and provide us with the following output:
Duas raízes Reais iguais: x1 = -2.0 e x2 = -2.0
Desejas realizar novo cálculo? [S/N]:
Please format the code. And put what you have tried.
– Paulo Marques
Try:
if D < 0: print('Não existe raízes') else: ...
– anonimo
That message from
continua = input(...)
confused. Hints that typing 'q' or 'Enter' the program will continue running– yoyo