【发布时间】:2019-09-19 15:35:00
【问题描述】:
尝试创建一个表格来比较计算数字平方根的不同方法。使用从 1 到 9 的整数测试脚本。
附加的脚本之前在 Idle 中运行正常,但现在我在导入制表时遇到了另一个错误。该脚本从未在 Jupyter Notebook 中成功运行。
当我在 Jupyter Notebook 中运行代码时,唯一的变化是 In [] 更改为 In [*]。
总的来说,我是 Jupyter Notebook/Idle/Python 的新手。我理解 In [*] 表示内核正在运行,但我不明白为什么计算会在 Idle 中运行正常而不是在 Jupyter Notebook 中运行。
这两个程序都与 Anaconda 一起安装。 Python 版本为 3.7.3。使用的所有模块都安装在 anaconda 中。操作系统是 Windows 10。
'''Function to compare two methods of finding the square root
of a value
'''This works in Idle but can not run in Jupyter Notebook. I don't understand why.
from tabulate import tabulate as tb
import numpy as np
import math
def mysqrt(a):
#x = float(input('What is a reasonable estimate for ' + str(a) + '?\n'))
#epsilon = float(input('What is your accuracy tolerance?\n'))
x = a/2
epsilon = 0.05
while True:
y = (x + a/x) / 2
if abs(y-x) < epsilon:
return y
break
x = y
def test_square_root():
headers = ["a","mysqrt(a)", "math.sqrt(a)","diff"]
table = np.zeros((9,4))
for i in range(1,9):
my_a = mysqrt(i)
math_a = math.sqrt(i)
diff = abs(my_a - math_a)
print(np.shape(table[0,:]))
table[i,:] = [i,my_a,math_a,diff]
print(tb(table,headers))
test_square_root()
当程序之前在 Idle 中运行时,它会输出一个带有 4 列 10 行的格式化表格,包括标题。
当我现在在空闲状态下运行脚本时,我收到错误消息:
Traceback (most recent call last):
File "C:\Users\aIDAN\Documents\Python Scripts\Think Python\test2.py", line 1, in <module>
from tabulate import tabulate as tb
ModuleNotFoundError: No module named 'tabulate'
在 Jupyter Notebook 中,我没有收到任何输出或反馈,只有 In [] 更改为 In [*])
感谢您的宝贵时间!
【问题讨论】: