Jump to content

Pyton NameError when instantiation


realeez

Recommended Posts

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: 90
Enter the second number: 78
90
78
Some error
Sorry, one of your values was not a valid integer.
Please input two intergers you wish to add:
 

Please advise

 

 

Thanks

Anes

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

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 by mikesta707
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.