Python 上使用 Spyder 的平方根函数

问题描述 投票:0回答:3

我刚刚开始在 Spyder 下使用 Python。我要做一个简单的平方根计算。我发现在Python中,你需要做一个“导入数学”。我在 Spyder 中执行此操作并收到错误:“数学”已导入但未使用。我尝试使用 sqrt() 但这引发了错误。我在这里缺少一些基本的东西。我做了很多搜索,但仍然无法解决这个问题。任何帮助将不胜感激。

谢谢! 克莱姆

python spyder
3个回答
0
投票
import math

print(math.sqrt(25))

或者

from math import sqrt

print(sqrt(25))

还有哦!为了更好的衡量:

import math

def sqrt(n):
    return "I am not the function you're looking for"

print(math.sqrt(25))
print(sqrt(25))  # see what happened here?


0
投票

我还不能发表评论,所以我会尽快删除这个答案。您写的是

math.sqrt()
还是只是写
sqrt()
?要调用 sqrt 函数,您必须输入
math.sqrt()
,除非您使用
from math import sqrt

导入 sqrt 函数

0
投票

我能想到3个选择。

1:

import math

n = 5

print(math.sqrt(n))

2:

from math import sqrt

n = 5

print(sqrt(n))

3:

n = 5

print(n ** 0.5)
© www.soinside.com 2019 - 2024. All rights reserved.