What is wrong with the Python code?? Bhaskara

Asked

Viewed 1,219 times

0

import math

def Bhaskara():
a = int(input('Digite um valor para A '))
b = int(input('Digite um valor para B '))
c = int(input('Digute um valor para C '))

#(-b +- {-b² - 4 * a * c} ) / 2 * c

operacao = -b
raiz = (operacao * operacao) - 4 * a * c
raiz = math.sqrt(raiz)
divisao = 2 * c
# (operacao +- raiz) / divisao
x1 = (operacao + raiz) / divisao
x2 = (operacao - raiz) / divisao
print('X1 = {}'.format(x1))
print('X2 = {}'.format(x2))
if(__name__ in '__main__'):
Bhaskara()

Consider the code indented correctly

  • 2

    tip: when paste code here use the button {} of the formatting bar after selecting all the code - so it keeps the correct indentation.

  • About the code - when you use int in the value of input, your program will only work for integer values - use float so that it can work with decimal numbers as well.

  • You have to put inside the def a tab ! otherwise it will not understand that it is inside the def

  • Thank you all very much!!! If you hadn’t talked, I couldn’t find !!!

  • It is not necessary to write an answer just to say "thank you". The best way to thank is accepting the answer that helped you. - Of Revision

3 answers

2

Its denominator is incorrect. The denominator in Bháskara’s formula is 2*a, and not 2*c:

# Errado: divisao = 2 * c
divisao = 2 * a  # Certo

Also, you will have problems when the roots are imaginary, because the math.sqrt only accepts 0 and positive numbers. I suggest you implement a check before trying to apply math.sqrt with a possibly negative number, or treat the exception.

  • 2

    @Jeffersonquesado but that’s right, the delta formula is b**2 - 4 * a * c and (-b)**2 == b**2.

  • I apologize for the lack of attention, and thank you for correcting me. I was led into error by (among other things like laziness of thinking and inattentiveness) comment account in question code

2

You are changing the quadratic coefficient a by the constant coefficient c in the denominator of the formula:

inserir a descrição da imagem aqui

You are also not checking whether the root is part of the set of real numbers by testing whether the discriminant (delta) is less than 0.

Follow your corrected and slightly modified solution for a better understanding and readability:

import math

def Bhaskara( a, b, c ):

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

    if ( delta < 0 ):
        return (None,None) # Raiz Negativa

    x = math.sqrt( delta )

    x1 = (-b + x) / (2 * a)
    x2 = (-b - x) / (2 * a)

    return (x1, x2)


if(__name__ in '__main__'):

    a = int(input('Digite um valor para A: '))
    b = int(input('Digite um valor para B: '))
    c = int(input('Digute um valor para C: '))

    x = Bhaskara( a, b, c )

    print('X1 = {}'.format(x[0]))
    print('X2 = {}'.format(x[1]))

Test #1:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Digite um valor para A: 2
Digite um valor para B: 7
Digute um valor para C: 3
X1 = -0.5
X2 = -3.0

Test #2:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Digite um valor para A: -3
Digite um valor para B: 2
Digute um valor para C: -1
X1 = None
X2 = None

0

It is not necessary to use module Math as it is equivalent to do delta**(1/2), the code below solves the equation where x1 and x2 are complex containing the real and imaginary part (if there is) of roots.

def Bhaskara():
    a = int(input('Digite um valor para A '))
    b = int(input('Digite um valor para B '))
    c = int(input('Digute um valor para C '))
    delta = (b ** 2) - (4 * a * c)
    x1 = (-b + delta ** (1 / 2)) / (2 * a)
    x2 = (-b - delta ** (1 / 2)) / (2 * a)
    print('X1 = {}'.format(x1))
    print('X2 = {}'.format(x2))
    print(type(x1))    # Verificação do tipo da variável    

if __name__ in '__main__':
    Bhaskara()

Browser other questions tagged

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