[英]How to write x^2 in my python code? Can someone give me suggestion about my code
二次方程计算器和我使用的代码运行不佳。 代码上有一些错误。
我已经尝试过使用基本数字,例如 1/2/3。 没有等式。 代码仍然不起作用。 实际起作用的是仅放置变量,仅此而已。 在我按回车键查看答案后,它说我的代码有一些错误。
print ("Quadratic Equation Calculator")
import math
print ("Enter the first variable : ")
first = float(input(''))
print ("Enter the second variable : ")
second = float(input(''))
print ("Enter the third variable : ")
third = float(input(''))
Answer1 = ((-1 * second) - math.sqrt((math.pow(second, 2) - 4.0*first*third))) / (2*first)
Answer2 = ((-1 * second) + math.sqrt((math.pow(second, 2) - 4.0*first*third))) / (2*first)
print (Answer1)
print (Answer2)
我希望能正确回答问题,这个方程计算器可用于实际方程和使用变量。 x 平方和 3x 之类的东西。
在 python x ^ 2 中,可以是 x ** 2、x * x 或 pow(x, 2)。 其他人给了你很好的建议,我想补充一些。 二次方程:ax^2 + bx + c = 0(调整使方程等于0,)有多项式项ax^2,bx; c,其系数为 ab 和 c 为常数项:则二次公式; (-b + sqrt(b ^ 2 - 4 * a * c)) / 2a。 求解 x。
以上所有内容都正确地出现在您的代码中但是,如果解决方案停留在复数集合 {C} 中,您将遇到麻烦。
这可以通过衡量“判别式”轻松解决。
判别式是 b^2 - 4ac,并且
考虑到上述情况,代码应如下所示:
import math
print ("Quadratic Equation Calculator")
a = float(input("Enter the coefficient of term `x ^ 2` (degree 2), [a]: "))
b = float(input("Enter the coefficient of term `x` (degree 1), [b]: "))
c = float(input("Enter the constant term (degree 0), [c]: "))
discriminant = pow(b, 2) - 4.0 * a * c
if discriminant == 0:
root1 = root2 = (-1 * b) / (2 * a)
elif discriminant < 0:
root1 = ((-1 * b) - math.sqrt(-discriminant) * 1j) / (2 * a)
root2 = ((-1 * b) + math.sqrt(-discriminant) * 1j) / (2 * a)
else:
root1 = ((-1 * b) - math.sqrt(discriminant)) / (2 * a)
root2 = ((-1 * b) + math.sqrt(discriminant)) / (2 * a)
print (root1)
print (root2)
类似的 SO 答案: https://stackoverflow.com/a/49837323/8247412
下面我更改了代码以支持 pythonic 编程,因为 numpy 可以强大地找到多项式(二次和更高阶)方程的根。 numpy.roots
import numpy as np
print ("Quadratic Equation Calculator")
a = float(input("Enter the coefficient of term `x ^ 2` (degree 2), [a]: "))
b = float(input("Enter the coefficient of term `x` (degree 1), [b]: "))
c = float(input("Enter the constant term (degree 0), [c]: "))
coeffs = [a, b, c] # or d, e and so on..
roots = np.roots(coeffs)
print (roots)
看起来您正试图找到二次 function y = a*x^2 + b*x + c
的根。 取决于a
、 b
和c
的值。 (请注意,您应该使用这些变量名称而不是first
、 second
和third
,因为它们是常用的数学名称。 )
根据a
、 b
和c
的值,根可能是复数。 我建议你从一些你知道会给出真实而不是复杂解决方案的值开始。
在 Python 中,当您尝试取负数的平方根时,会出现错误。 如果你想能够计算复数根,你需要学习如何在 Python 中使用复数。
尝试这个。 我建议对变量使用较短的名称。 如果您尚未安装 numpy,您可以将“import numpy as np”替换为“import cmath”,并将“np.lib.scimath”替换为“cmath”。 QES 代表二次方程求解器。
#Import package
import numpy as np
#Define a new function called qes
def qes(a1,b1,c1):
ans1=((-1*b1)+np.lib.scimath.sqrt((b1**2)-(4*a1*c1)))/(2*a1)
ans2=((-1*b1)-np.lib.scimath.sqrt((b1**2)-(4*a1*c1)))/(2*a1)
return ans1,ans2
#With the defined function perform a calculation and print the answer
ans1,ans2=qes(1,2,1)
print('Answer 1 is: ',ans1,' Answer 2 is: ',ans2)
感谢大家的支持。 但我已经回答了我的问题。 我想我没有详细说明我遇到的问题。 但我知道我使用什么代码。 我制作了这个代码,不仅使用了二次方程问题,而且它会帮助你找到二次方程的最小点。 编码:
import math
print("Quadratics Equation Calculator")
repeat = "yes"
while repeat.lower() == "yes":
V = float(input("Enter how many Variables do you have in the question: "))
print(V)
if V == 3:
a = float(input("Enter the first variable: "))
print(a)
b = float(input("Enter the second variable: "))
print(b)
c = float(input("Enter the third variable: "))
print(c)
root_1 = ((-1 * b) + math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)
root_2 = ((-1 * b) - math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)
print(f"The first result is {root_1} to {round(root_1)}")
print(f"The second result is {root_2} to {round(root_2)}")
graph = str(input("Want minimum point: "))
if graph == "yes":
x = ((b / 2) * -1)
y = c - b ** 2/4*a
print(f"The minimum point is ({x}, {y})")
elif graph == "no":
repeat = str(input("Do you wish to continue?: "))
if repeat == "no":
break
else:
repeat = str(input("Do you wish to continue?: "))
if repeat == "no":
break
elif V == 2:
a = float(input("Enter the first variable: "))
print(a)
b = float(input("Enter the second variable: "))
print(b)
root_1 = ((-1 * b) + math.sqrt((b ** 2) - (4 * a * 0))) / (2 * a)
root_2 = ((-1 * b) - math.sqrt((b ** 2) - (4 * a * 0))) / (2 * a)
print(f"The first result is {root_1} to {round(root_1)}")
print(f"The second result is {root_2} to {round(root_2)}")
else:
print("INVALID ERRORS, CHECK AGAIN YOUR VARIABLES")
print("Type yes or no.")
repeat = str(input("Do you wish to continue?: "))
if repeat == "no":
break
谢谢你们给我的所有答案以及它的公式。
X^2 是 python 中的按位异或运算符。您可以在 python 中将等式重写为 x**2(exponential),x*x 或 pow(x,2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.