Exercise - Calculation of bhaskará

Asked

Viewed 90 times

0

a=int(input("Digite o valor de A: "))

b=int(input("Digite o valor de B: "))

c=int(input("Digite o valor de C: "))

delta=float(((b**2)-4*a*c))

r1=float((-b +(delta ** (1/2))/2)

r2=float((-b -(delta ** (1/2))/2)


if delta > 0

    print("Temos duas raizes reais e distintas. \nRaiz 1: {} \nRaiz 2:{}".format(r1,r2))

elif delta = 0

    print("Temos duas raízes iguais. \nRaiz 1: {} \nRaiz 2: {}".format(r1,r2))
elif delta < 0

    print("Raizes inexistentes.")

Error:

R2=float((-b -(delta ** (1/2))/2)

 ^

Syntaxerror: invalid syntax

What would be the mistake? he says it’s on line 6, which is R2, but I don’t see anything that interferes with the code.

1 answer

2


The problem is that on the line of r1 and of r2 you didn’t close the parentheses of float. See below:

Original:

r1=float((-b +(delta ** (1/2))/2)
r2=float((-b -(delta ** (1/2))/2)

Modified:

r1=float((-b +(delta ** (1/2))/2))
r2=float((-b -(delta ** (1/2))/2))

Other mistakes:

In the conditional block, you forgot to start the code blocks with ":" and in the first block elif you placed an assignment signal (=) instead of using a comparison operator (==).

The right thing should be:

if delta > 0:
    print("Temos duas raizes reais e distintas. \nRaiz 1: {} \nRaiz 2:{}".format(r1,r2))

elif delta == 0:
    print("Temos duas raízes iguais. \nRaiz 1: {} \nRaiz 2: {}".format(r1,r2))

elif delta < 0:
    print("Raizes inexistentes.")

Another problem that your code has on lines 17 and 19 is that in some calculations you will get the error TypeError: can't convert complex to float. This is because in some calculations, the delta may have a negative value and the program will calculate it using potentiation. To solve this, you must do a delta check before calculating the roots.

  • Jean, also mention the problem with the complex numbers, do not solve this problem! but show the way to fix or where to look for the solution.

  • Problem with complex numbers ? What exactly are you talking about ?

  • https://onlinegdb.com/BJJEUWSXS

  • 1

    Putz I hadn’t noticed that because I hadn’t executed the kkk code. Thanks for warning, I’ll edit the answer.

  • Thanks for the personal tips, I will study the language better.

Browser other questions tagged

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