你好,我是Python的新手。在
变量的声明是令人沮丧的,因为它应该是容易的,但我有如此困难的时间使这项工作。。
我读过其他的stackoverflow问题,显然Python中没有初始化,我需要关键字:global before variable在不同的地方使用它。。在@app.route('/calculate', methods = ['GET'])
def calculate():
# get value from html by request.args.get()
选项1。在
^{pr2}$
选项2。在if weightT1 != weightT2:
global newWeightForSecondItem = convert(weightT1, weightT2, weight2)
都不管用。。
当我在下面进行这样的计算时,我得到一个错误:NameError:name'newweightferseconditem'没有定义。在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()