1

Hello I am a newbie to Python.

Declaration of variables is frustrating as it should be easy but I have such hard time to make this work.. I've read other stackoverflow questions and apparently there is no such thing as initialization in Python and I need keyword: global before variable to use it in different places..

 @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)

Neither works.. When I do such calculation below, I get an error: NameError: name 'newWeightForSecondItem' is not defined.

   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()
5
  • 2
    There is no variable declaration in Python, so that likely explains why you are finding it difficult. Commented Mar 24, 2017 at 22:07
  • 2
    Can you post a more complete set of your code... this is a scoping issue and you've shown us none of your scope. Commented Mar 24, 2017 at 22:09
  • @TemporalWolf based on your advice, I've added the overall code. Thank you. Commented Mar 24, 2017 at 22:17
  • Have you called calculate() before you got the NameError? If not newWeightForSecondItem would never have been initialized. To be on the safe side you can init newWeightForSecondItem outside of the function. Commented Mar 24, 2017 at 22:39
  • @DahliaSR yes. calculate() is called first. Commented Mar 25, 2017 at 1:23

2 Answers 2

1

I spent a lot time to figure out why I got this error. NameError: name 'newWeightForSecondItem' is not defined.

However, that was not the main issue. I forgot to convert string to float datatype for newWeightForSecondItem. After I changed it to float(newWeightForSecondItem), it works. This was a very easy mistake and I think python's error was not very helpful.

Thank you for all the comments, everyone.

Sign up to request clarification or add additional context in comments.

Comments

0

if you want to have newWeightForSecondItem as a global variable perhaps you can try this:

 newWeightForSecondItem = None

 @app.route('/calculate', methods = ['GET'])      
 def calculate():
     global newWeightForSecondItem
     if weightT1 != weightT2:
         newWeightForSecondItem = convert(weightT1, weightT2, weight2)

You declare/initialize the global variable, and then you can use it inside the function

5 Comments

Thank you for the comment. I don't get the same error anymore but now I get NonType in the calculation..
Instead of newWeightForSecondItem = None, you should initialize the value before using it. maybe set it to newWeightForSecondItem = 0.
@GabrielFalcones which will yield a ZeroDivisionError, because he's calling it before assigning to it.
@GabrielFalcones yes.. TemporalWolf is correct. I get ZeroDivisionError: float division by zero
You get that error because you are calling that variable as the denominator of a division, in that case you shouldnt initialize the variable with 0, but with a different number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.