函数调用出命名空间

Function call out of namespace

提问人:Draculin 提问时间:2/3/2022 更新时间:2/3/2022 访问量:53

问:

此代码位于一个函数中,该函数借助字典中的键调用另一个函数 start() 根据用户输入 opt

    ht = b[1]
    wt = a[len(a) - 1][1]
    age = b[0]

    calburn_lst = [i[3] for i in a]
    wt_lst = [i[1] for i in a]
    bp_lst = [i[4] / i[5] for i in a]
    date_lst = [i[0] for i in a]
    dia_lst = [i[6] for i in a]

    bmi_lst = [round(wts / (ht * ht), 2) for wts in wt_lst]

    for i in range(t):

        u = start()
        print(opt[u]())

例如,如果我调用函数:hwc()

def hwc():
print(
    "Healthy Weight Calculator is the healthy weight range of a person based on his/her BMI\n\n"
)

t = True
while t == True:
    desc = input("Do you wish to see the graph as well? (Y/N)\t").lower()

    if desc == "y":
        plt.plot(date_lst, wt_lst)
        plt.title("Weight vs Date graph")
        plt.xlabel("Date")
        plt.ylabel("Weight")
        plt.show()
        t = False
    elif desc == "n":
        pass
    else:
        print("Invalid Input")

wt_l, wt_h = 18.5 * ht * ht, 25 * ht * ht
return f"{wt_l}, {wt_h} is your healthy weight range\n\nYour weight: {wt}"

我收到此错误:

plt.plot(date_lst, wt_lst)
NameError: name 'date_lst' is not defined

如何在函数的命名空间中包含初始数据?

Python 调试 范围 命名空间

评论


答:

2赞 Rahul 2/3/2022 #1

我建议更新您的内容以包含所需的参数。hwc

def hwc(date_lst, wt_lst):
    ...

然后这样称呼它:hwc(date_lst, wt_lst)