簡體   English   中英

如何在我的 python 代碼中寫入 x^2? 有人可以給我關於我的代碼的建議嗎

[英]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,並且

  • 如果判別式 = 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)

類似的 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的根。 取決於abc的值。 請注意,您應該使用這些變量名稱而不是firstsecondthird ,因為它們是常用的數學名稱。

根據abc的值,根可能是復數。 我建議你從一些你知道會給出真實而不是復雜解決方案的值開始。

在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM