Python 在/local变量处取消绑定localerror';键';分配前参考
这是我的代码我面临这个错误我无法找到错误修复错误showPython 在/local变量处取消绑定localerror';键';分配前参考,python,django,Python,Django,这是我的代码我面临这个错误我无法找到错误修复错误show def get_key(res_by): if res_by == '#1': key = 'transaction_id' elif res_by == '#2': key = 'created' return key def get_chart(chart_type, data, results_by, **kwargs): plt.switch_backend(
def get_key(res_by):
if res_by == '#1':
key = 'transaction_id'
elif res_by == '#2':
key = 'created'
return key
def get_chart(chart_type, data, results_by, **kwargs):
plt.switch_backend('AGG')
fig = plt.figure(figsize=(10,4))
key = get_key(results_by)
这就是代码问题是,如果两个条件都失败,
键
没有被分配任何值,但您返回键
。因此,您需要在else
案例中指定一个值:
def get_key(res_by):
if res_by == '#1':
key = 'transaction_id'
elif res_by == '#2':
key = 'created'
else:
key = something # ← specify a value if the two conditions fail
return key
def get_键(res_by):
如果res#u by=='#1':
key='transaction\u id'
elif res_by=='#2':
键='created'
其他:
key=something#←如果两个条件失败,请指定一个值
返回键如果resu by
不是1或怎么办?它不会被定义。试试这个
def get_key(res_by):
if res_by == '#1':
key = 'transaction_id'
elif res_by == '#2':
key = 'created'
else:
key = None
return key
如果resu by
不是#1或#2怎么办?两个条件都不匹配,键未设置。