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()
添加回答
舉報