您好,我是Python的新手。Python变量 - 名称''未定义
变量声明令人沮丧,因为它应该很容易,但我有这样的困难时间,使这项工作.. 我读过其他stackoverflow的问题,显然没有这样的事情,在Python中的初始化,我需要关键字:全球变量之前使用它在不同的地方..
@app.route('/calculate', methods = ['GET'])
def calculate():
# get value from html by request.args.get()
Option1。
global newWeightForSecondItem
if weightT1 != weightT2:
newWeightForSecondItem = convert(weightT1, weightT2, weight2)
选项2
if weightT1 != weightT2:
global newWeightForSecondItem = convert(weightT1, weightT2, weight2)
无论是工程.. 当我做这样的计算之下,我得到一个错误:NameError:名字 'newWeightForSecondItem' 没有定义。
if discountT2 == "percentage":
finalPrice2 = float((float(price2) - (float(price2) * float(discount2)))/newWeightForSecondItem)
elif discountT2 == "dollar":
finalPrice2 = float((float(price2) - float(discount2))/newWeightForSecondItem)
def convert(weightT1, weightT2, weight2):
# converting calculation here
return weight2
# main method
if __name__ == '__main__':
app.debug = True
app.run()
在Python中没有变量声明,所以这可能解释了为什么你发现它很困难。 –
你可以发布一组更完整的代码......这是一个范围界定问题,你没有向我们展示你的范围。 – TemporalWolf
@TemporalWolf基于您的建议,我添加了整体代码。谢谢。 –