What's wrong with Python code ?? Bhaskara [closed]

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 indented code correctly

    
asked by anonymous 07.07.2018 / 19:37

3 answers

2

Your denominator is incorrect. The denominator in the Bháskara formula is 2*a , not 2*c :

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

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

    
07.07.2018 / 19:47
2

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

Youarealsonotverifyingthattherootispartofthesetofrealnumbersbytestingwhetherthediscriminatorislessthan0.

Followyourcorrectedandslightlymodifiedsolutionforbetterunderstandingandreadability:

importmathdefBhaskara(a,b,c):delta=(b**2)-(4*a*c)if(delta<0):return(None,None)#RaizNegativax=math.sqrt(delta)x1=(-b+x)/(2*a)x2=(-b-x)/(2*a)return(x1,x2)if(__name__in'__main__'):a=int(input('DigiteumvalorparaA:'))b=int(input('DigiteumvalorparaB:'))c=int(input('DiguteumvalorparaC:'))x=Bhaskara(a,b,c)print('X1={}'.format(x[0]))print('X2={}'.format(x[1]))

Test#1:

DigiteumvalorparaA:2DigiteumvalorparaB:7DiguteumvalorparaC:3X1=-0.5X2=-3.0

Test#2:

Digite um valor para A: -3
Digite um valor para B: 2
Digute um valor para C: -1
X1 = None
X2 = None
    
08.07.2018 / 06:47
0

It is not necessary to use the math module because it is equivalent to 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 the 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()
    
07.07.2018 / 21:20
Encoding problems ___ ___ erkimt The third parameter filter_input is mandatory in PHP? ______ qstntxt ___
%pre%

I took a look at the PHP manual but did not say anything, I saw a guy not using the third parameter and then I was in doubt if it is mandatory or not

    
______ azszpr313414 ___

It's not, check it out in the documentation: link

%pre%

These %code% indicate optional arguments, in PHP there are predefined arguments, when you omit in your use php passes the default value, which in the specific case of this function would be %code% , the code you posted shows that the person has changed %code% by %code% , that is, each one is for one thing, just see the values supported in link

As it says in the doc:

  

If omitted, %code% will be used, which is equivalent to %code% . This will result in no filtering taking place by default.

     

If omitted, it will use %code% , which is equivalent to %code% . This will result in an unfiltered value.

Just to note, %code% is part of the link , this filter in the case escapes / converts characters such as: %code% , %code% , %code% , among others that the ASCII value is less than 32, ie something like:

%pre%

Will print this:

%pre%

What are HTML entities, which when rendered on the page actually display %code% , but without affecting HTML.

    
______ azszpr313406 ___

The documentation of the %code% function says the following about the third parameter:

  

"If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default."

That is, the third parameter is not required and can be omitted. Although this does not filter the value in fact.

    
___