【问题标题】:Library and Module Question - Python Beginner库和模块问题 - Python 初学者
【发布时间】:2022-11-16 13:31:14
【问题描述】:

在尝试学习如何使用数学模块时,我一直遇到一些问题。我不知道学习材料是否过时或者我只是做错了什么,但每次使用正在导入的数学模块的示例时,我似乎都跟不上。有没有人对使用 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

这里的代码只是从我展示的示例中复制和粘贴,因为我在尝试自己写出来时遇到了同样的问题,但这仍然行不通。

【问题讨论】:

    标签: python math module pycharm


    【解决方案1】:

    你的代码:

    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)
    

    在 sideS 和 side 之间有“=”对我来说是一个错误。

    完整代码:

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多