Python 求解二次方程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15398427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Solving Quadratic Equation
提问by
My program doesn't seem to give me the right solutions. Sometimes it does, sometimes it doesn't. I can't find my error. Any Suggestions?
我的程序似乎没有给我正确的解决方案。有时会,有时不会。我找不到我的错误。有什么建议?
import math
a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")
d = b**2-4*a*c # discriminant
if d < 0:
print "This equation has no real solution"
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print "This equation has one solutions: ", x
else:
x1 = (-b+math.sqrt(b**2-4*a*c))/2*a
x2 = (-b-math.sqrt(b**2-4*a*c))/2*a
print "This equation has two solutions: ", x1, " and", x2
采纳答案by Blender
This line is causing problems:
此行导致问题:
(-b+math.sqrt(b**2-4*a*c))/2*a
x/2*ais interpreted as (x/2)*a. You need more parentheses:
x/2*a被解释为(x/2)*a。您需要更多括号:
(-b + math.sqrt(b**2 - 4*a*c)) / (2 * a)
Also, if you're already storing d, why not use it?
另外,如果您已经在存储d,为什么不使用它呢?
x = (-b + math.sqrt(d)) / (2 * a)
回答by Hyman Tomlinson
Here you go this should give you the correct answers every time!
在这里,你每次都应该给你正确的答案!
a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))
d = b**2-4*a*c # discriminant
if d < 0:
print ("This equation has no real solution")
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print ("This equation has one solutions: "), x
else:
x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
print ("This equation has two solutions: ", x1, " or", x2)
回答by Organis
# syntaxis:2.7
# solution for quadratic equation
# a*x**2 + b*x + c = 0
d = b**2-4*a*c # discriminant
if d < 0:
print 'No solutions'
elif d == 0:
x1 = -b / (2*a)
print 'The sole solution is',x1
else: # if d > 0
x1 = (-b + math.sqrt(d)) / (2*a)
x2 = (-b - math.sqrt(d)) / (2*a)
print 'Solutions are',x1,'and',x2
回答by Evan Haston
import math
a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))
d = b**2-4*a*c # discriminant
if d < 0:
print ("This equation has no real solution")
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print (("This equation has one solutions: "), x)
#add the extra () above or it does not show the answer just the text.
else:
x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
print ("This equation has two solutions: ", x1, " or", x2)
回答by ???? ???????
<code>
import cmath
import math
print(" we are going to programming second grade equation in python")
print(" a^2 x + b x + c =0")
num1 = int(input(" enter A please : "))
num2 = int(input(" enter B please : "))
num3 = int(input(" enter c please : "))
v = num2*num2 - 4 *num1 * num3
print(v)
if v < 0 :
print("wrong values")
else:
print("root of delta =", v)
k= math.sqrt(v)
def two_sol(x,y) :
x_f= (-y + v)/(4*x)
x_s =(-y - v)/(4*x)
return x_f , x_s
def one_sol(x):
x_f = (-y + v) / (4 * x)
if v >0 :
print("we have two solution :" ,two_sol(num1,num2))
elif v == 0:
print( "we have one solution :" , one_sol(y))
else:
print(" there is no solution !!")
</code>
回答by bru1987
How about accepting complex roots as solutions?
接受复杂的根作为解决方案怎么样?
import math
# User inserting the values of a, b and c
a = float(input("Insert coefficient a: "))
b = float(input("Insert coefficient b: "))
c = float(input("Insert coefficient c: "))
discriminant = b**2 - 4 * a * c
if discriminant >= 0:
x_1=(-b+math.sqrt(discriminant))/2*a
x_2=(-b-math.sqrt(discriminant))/2*a
else:
x_1= complex((-b/(2*a)),math.sqrt(-discriminant)/(2*a))
x_2= complex((-b/(2*a)),-math.sqrt(-discriminant)/(2*a))
if discriminant > 0:
print("The function has two distinct real roots: ", x_1, " and ", x_2)
elif discriminant == 0:
print("The function has one double root: ", x_1)
else:
print("The function has two complex (conjugate) roots: ", x_1, " and ", x_2)
回答by itishree itish
give input through your keyboard
通过键盘输入
a=float(input("enter the 1st number : "))
b=float(input("enter the 2nd number : "))
c=float(input("enter the 3rd number : "))
calculate the discriminant
计算判别式
d = (b**2) - (4*a*c)
possible solution are
可能的解决方案是
sol_1 = (-b-(0.5**d))/(2*a)
sol_2 = (-b+(0.5**d))/(2*a)
print the result
打印结果
print('The solution are %0.f,%0.f'%(sol_1,sol_2))
回答by Ashish Yadav
Below is the Program to Solve Quadratic Equation.
下面是求解二次方程的程序。
For Example: Solve x2 + 3x – 4 = 0
例如:求解 x2 + 3x – 4 = 0
This quadratic happens to factor:
这个二次方碰巧因数:
x2 + 3x – 4 = (x + 4)(x – 1) = 0
x2 + 3x – 4 = (x + 4)(x – 1) = 0
we already know that the solutions are x = –4 and x = 1.
我们已经知道解是 x = –4 和 x = 1。
# import complex math module
import cmath
a = 1
b = 5
c = 6
# To take coefficient input from the users
# a = float(input('Enter a: '))
# b = float(input('Enter b: '))
# c = float(input('Enter c: '))
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))


