如何在python代码中编写x^2?有人能给我关于代码的建议吗

如何在python代码中编写x^2?有人能给我关于代码的建议吗,python,symbolic-math,Python,Symbolic Math,二次方程计算器和我使用的代码工作不好。 代码中有一些错误 我已经试过基本数字,比如1/2/3。没有方程式。代码仍然不起作用。 真正起作用的是只放变量,仅此而已。 在我按下回车键查看答案后,它说我的代码有一些错误 print ("Quadratic Equation Calculator") import math print ("Enter the first variable : ") first = float(input('')) print ("Enter the second va

二次方程计算器和我使用的代码工作不好。 代码中有一些错误


我已经试过基本数字,比如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以及类似的东西。

看起来你在试图找到一个二次函数的根
y=a*x^2+b*x+c
。取决于
a
b
c
的值。(注意,您应该使用这些变量名,而不是
第一个
第二个
第三个
,因为它们是常用的数学名称。

根据
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)

在pythonx^2中,可以是x**2、x*x或pow(x,2)。 其他人给了你很好的建议,我想补充几点。 二次方程:ax^2+bx+c=0(调整使方程等于零!) 具有多项式项ax^2,bx,c;其系数为a,b。c是常数项。 二次公式为:(-b+sqrt(b^2-4*a*c))/2a;解x

以上所有内容都正确地显示在您的代码中 但是,如果解停留在复数集{C}中,则会遇到问题

这可以通过测量“判别式”来轻松解决

判别式为b^2-4ac,且

  • 如果判别式=0,则只有一个解
  • 如果判别式>0,则存在两个实解
  • 如果判别式<0,则有两个复解
考虑到上述条件,代码应如下所示:

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)

谢谢大家的支持。但我已经回答了我的问题。我想我没有详细说明我遇到的问题。但我知道我用的是什么代码。我制作了这段代码,不仅用于解决二次方程问题,还将帮助您找到二次方程的最小点。 守则:

    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*x
“代码仍然不起作用。”它做什么?您希望它做什么呢?更通用的语法是
x**2
。这就是说,我不确定这是一个好的SO问题——当然,代码远不止是一个极小值(这应该是能够证明一个非常具体的错误的尽可能短的东西,预期和实际输出都很清楚)。“…代码上有一些错误。”这不是一个好的错误描述,至少在这一点上是…的一部分“最短可能”是指,如果您的问题与用户输入无关,则根本不进行用户输入,只需硬编码一些显示问题的特定值;这还可以让您显示这些特定值的预期和实际输出。另请参阅“修剪技巧”“第at节。这似乎很大程度上是推测性的——如果我们要求OP显示具体的输入和由此产生的具体错误,并且他们确认了手头的推测,那么这可能是一个明确的点答案,但现在的猜测可能更适合作为一个评论而不是答案。@charlesduff是的,这是我的猜测,我在评论中要求澄清。@CharlesDuffy仔细观察这个问题,OP写道“我已经尝试过基本数字,比如1/2/3。”我想这意味着这些是他尝试过的输入值。如果是这样,那么得到的二次函数有复根。这与原始问题中的代码有何不同?我看到的唯一区别是变量名。我同意以这种方式缩短它们是很有意义的,但它不能解决任何错误。在我的系统上运行良好。我不知道您遇到了什么错误。我将我的答案更新为函数,改为使用numpy。您使用的输入与OP给出的不同。调用
qes(1,2,3)
时会发生什么?另外,我认为为一个清晰的初学者问题引入numpy只会让他们感到困惑。OP想要回虚数吗?我想OP只是想解课本上提供的简单的二次方程。在这个上下文中使用numpy与使用math没有什么不同。这也让OP在以后学习更高级的函数时走上了正轨。我再次编辑它以使用np.lib.scimath.sqrt,这样就可以返回复杂的根。
    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