Bonjour, je suis un débutant à Python.Python Variable - nom '' n'est pas défini
La déclaration des variables est frustrante car elle devrait être facile mais j'ai beaucoup de mal à faire fonctionner cela .. J'ai lu d'autres questions de stackoverflow et apparemment il n'y a pas d'initialisation en Python et j'ai besoin de mot-clé: global avant variable pour l'utiliser à différents endroits.
@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)
Option 2.
if weightT1 != weightT2:
global newWeightForSecondItem = convert(weightT1, weightT2, weight2)
ne travaille .. Quand je fais ce calcul ci-dessous, je reçois une erreur: NameError: nom 'newWeightForSecondItem' est pas défini.
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()
Il n'y a pas de déclaration de variable en Python, ce qui explique probablement pourquoi vous trouvez cela difficile. –
Pouvez-vous poster un ensemble plus complet de votre code ... c'est un problème de portée et vous nous avez montré votre champ d'application. – TemporalWolf
@TemporalWolf basé sur vos conseils, j'ai ajouté le code général. Je vous remercie. –