猿问

函数参数:ab 未定义

当我运行这段代码时,它说ab没有定义。问题是什么?


import math

def taylor(ab):

    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(ab)


慕虎7371278
浏览 185回答 2
2回答

开心每一天1111

import mathdef 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 mathdef 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)

慕田峪4524236

您试图在定义变量ab之前使用它。您不能将未定义的变量传递给函数。在taylor()不需要声明任何参数。删除参数应该可以解决您的问题:import mathdef 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()
随时随地看视频慕课网APP

相关分类

Python
我要回答