在嘗試學習如何使用數學模塊時,我一直遇到一些問題。我不知道學習材料是否過時或者我只是做錯了什么,但每次使用正在匯入的數學模塊的示例時,我似乎都跟不上。有沒有人對使用 pyCharm 中的數學模塊有任何提示?
這是我當前代碼的示例;
from math import sqrt
print("Welcome to the Hypotenuse calculator!")
sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))
c = sqrt(sideA ** 2 = sideB ** 2)
print("Thank you! The length of the Hypotenuse is ", str(c))
c = sqrt == sideA ** 2 = sideB ** 2
^
SyntaxError: cannot assign to comparison
Process finished with exit code 1
這里的代碼只是從我正在展示的示例中復制粘貼,因為我在嘗試自己寫出來時遇到了同樣的問題,但這仍然行不通。
uj5u.com熱心網友回復:
你的代碼:
from math import sqrt
print("Welcome to the Hypotenuse calculator!")
sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))
c = sqrt(sideA ** 2 = sideB ** 2)
print("Thank you! The length of the Hypotenuse is ", str(c))
修改后的代碼:
我不得不為自己匯入數學。然后我改變了:
c = sqrt(sideA ** 2 = sideB ** 2)
至:
c = sqrt(sideA ** 2 sideB ** 2)
在 sideA 和 sideB 之間使用“=”對我造成了錯誤。
完整代碼:
import math
from math import sqrt
print("Welcome to the Hypotenuse calculator!")
sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))
c = sqrt(sideA ** 2 sideB ** 2)
print("Thank you! The length of the Hypotenuse is ", round(c,2)) #round() function will print any decimal places you want. Here it is set 2.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/534447.html
