使用Python 3.8在visual studio代码中导入统计信息找不到中间值

2024-10-02 12:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以使用Visual Studio代码Python 3.8.0导入统计信息。我甚至可以看到intellisense选项的中位数、中位数低等等

import statistics as st

numbers  = [0, -6, 10, 5, 8, 2, -12, 11, -2]

print(numbers)

numbers.sort()

print(numbers)

#[-12, -6, -2, 0, 2, 5, 8, 10, 11]
print("\nmedian of the numbers")
print(st.median(numbers))

然而,当我执行代码时,我在VisualStudio代码中得到了以下内容。 我做错了什么

[0, -6, 10, 5, 8, 2, -12, 11, -2]
[-12, -6, -2, 0, 2, 5, 8, 10, 11]

median of the numbers
Traceback (most recent call last):
  File "d:/ProgrammingSource/Python/PracticeVarious/pracNumbers.py", line 14, in <module>
    print(st.median(numbers))
AttributeError: module 'statistics' has no attribute 'median'

我确实把我的代码用在了Anaconda/Spyder和Anaconda/Jupyter中,它实际上起了作用。Jupyter没有Intellisense,Spyder有时也不会显示不同的选项。可视化代码让我可以看到不同的选项

我并不是要讨论使用哪种Python IDE。只是想找出我在VisualStudio代码中键入的错误

我确实发现了一个类似的问题,但此人没有具体说明他们使用了哪种Python。 我的3.8.0版本如下:

PS D:\ProgrammingSource\Python\PracticeVarious> python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Tags: ofthe代码选项anacondamedianmodulest
1条回答
网友
1楼 · 发布于 2024-10-02 12:26:54

当前工作目录中可能已经有一个名为statistics.py的文件。检查此目录中是否存在-

D:\ProgrammingSource\Python\PracticeVarious

它显示错误,因为它不是导入默认统计信息模块,而是导入statistics.py。要解决此问题,只需重命名名为statistics.py的文件

相关问题 更多 >

    热门问题