命名空间外的函数调用
Function call out of namespace
此代码位于一个函数内,该函数根据用户输入借助字典 opt 中的键调用另一个函数 start()
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
如何在函数的命名空间中包含初始数据?
我建议更新您的 hwc
以包含所需的参数。
def hwc(date_lst, wt_lst):
...
然后这样称呼它:hwc(date_lst, wt_lst)
此代码位于一个函数内,该函数根据用户输入借助字典 opt 中的键调用另一个函数 start()
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
如何在函数的命名空间中包含初始数据?
我建议更新您的 hwc
以包含所需的参数。
def hwc(date_lst, wt_lst):
...
然后这样称呼它:hwc(date_lst, wt_lst)