realeez Posted November 7, 2015 Share Posted November 7, 2015 Dear Friends , I am working on a simple python instantiation program given below. I got name error. class adder: result = 0 def __init__(self, number1, number2): #self.result = int( number1 ) + int( number2 ) self.number1 = number1 self.number2 = number2 def giveResult(self): #return str(self.result) return self.number1 + self.number2 endIt = False while ( endIt == False ): print "Please input two intergers you wish to add: " number1 = raw_input( "Enter the first number: " ) number2 = raw_input( "Enter the second number: " ) print number1 print number2 try: thistime=adder(int(number1),int(number2)) except NameError: print "Some error" print "Sorry, one of your values was not a valid integer." continue print "Your result is: " + thistime.giveResult() goagain = raw_input( "Do you want to eXit or go again? ('X' to eXit, anything else to continue): " ) if ( goagain == "x" or goagain == "X" ): endIt = True I got error as Enter the first number: 90Enter the second number: 789078Some errorSorry, one of your values was not a valid integer.Please input two intergers you wish to add: Please advise Thanks Anes Quote Link to comment https://forums.phpfreaks.com/topic/299329-pyton-nameerror-when-instantiation/ Share on other sites More sharing options...
mikesta707 Posted December 3, 2015 Share Posted December 3, 2015 (edited) Not sure exactly why you were getting that error. I did the following and it finally worked: class adder: result = 0 def __init__(self, number1, number2): #self.result = int( number1 ) + int( number2 ) self.number1 = number1 self.number2 = number2 def giveResult(self): #return str(self.result) return str(self.number1 + self.number2) endIt = False while ( endIt == False ): print ("Please input two intergers you wish to add: ") number1 = input( "Enter the first number: " ) number2 = input( "Enter the second number: " ) print (number1) print (number2) try: thistime=adder(int(number1),int(number2)) except NameError as e: print ("Some error", e) print ("Sorry, one of your values was not a valid integer.") continue print ("Your result is: " + thistime.giveResult()) goagain = input( "Do you want to eXit or go again? ('X' to eXit, anything else to continue): " ) if ( goagain == "x" or goagain == "X" ): endIt = True Edit: weird, all the other text I wrote seems to have disappeared. The gist was I used these lines to help narrow down the issue: except NameError as e: print ("Some error", e) print ("Sorry, one of your values was not a valid integer.") continue doing Except NameError as e helped since I could print e and see why exactly you were getting the NameError exception. I also had to use input rather than raw_input since I am probably using a different version of Python than you are (I am using 3.4.2, you are probably using an older version) These lines also weren't indented right print ("Sorry, one of your values was not a valid integer.") continue If you weren't getting the exception, those two lines would ALWAYS run, resulting in you never getting the actual result of the two numbers. I also had to convert your result into a string in your giveResult method like so: return str(self.number1 + self.number2) Hope this helps Edited December 3, 2015 by mikesta707 Quote Link to comment https://forums.phpfreaks.com/topic/299329-pyton-nameerror-when-instantiation/#findComment-1527510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.