2 回答
TA贡献1836条经验 获得超13个赞
import math
def taylor():
ab = float(input("What is the parameter precision? :"))
print(f"Calling taylor: {(ab)}")
num = 0
x = 1
n = 0
y = 1
while abs(math.pi - num) > ab:
num = num + (4 * (x / y))
x = x * -1
y += 2
n = n + 1
print(f"Calling basel : {(ab)} returns {(num), (n)}")
taylor()
或者:
import math
def taylor(ab):
print(f"Calling taylor: {(ab)}")
num = 0
x = 1
n = 0
y = 1
while abs(math.pi - num) > ab:
num = num + (4 * (x / y))
x = x * -1
y += 2
n = n + 1
print(f"Calling basel : {(ab)} returns {(num), (n)}")
ab = float(input("What is the parameter precision? :"))
taylor(ab)
TA贡献1875条经验 获得超5个赞
您试图在定义变量ab之前使用它。您不能将未定义的变量传递给函数。在taylor()不需要声明任何参数。删除参数应该可以解决您的问题:
import math
def taylor():
ab = float(input("What is the parameter precision? :"))
print(f"Calling taylor: {(ab)}")
num = 0
x = 1
n = 0
y = 1
while abs(math.pi - num) > ab:
num = num + (4 * (x / y))
x = x * -1
y += 2
n = n + 1
print(f"Calling basel : {(ab)} returns {(num), (n)}")
taylor()
添加回答
举报