Adding integers

need help with this simple code

Page 1 of 1

3 Replies - 4666 Views - Last Post: 05 July 2009 - 07:54 AM Rate Topic: -----

#1 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Adding integers

Post icon  Posted 02 July 2009 - 04:56 PM

Hi all,

In the last couple of months i got myself into programming. At first I wanted to learn java, so I spent a several spent months, along money trying learn that, but then I found myself making to many errors and overall not really understaning it. I decided to look into other programming languges, and found that python, which is somewhat similar to java, but seems much easier to use and write codes. With Python I found my self not worrying aboout classes, brackets and long process in defining objects. Python lanaguage is much easier for me to learn then java. Well any how, I wrote this simple code, but I keep getting this error message, can some tell what this means and how to correct it. The code I wrote is below the error message.

P.S.... I'm Using python 3.1 ide

erroer message
"
Traceback (most recent call last):
File "C:/Python31/adding.py", line 2, in <module>
integer1 = raw_input( 'Enter First Number: \n' )
NameError: name 'raw_input' is not defined
"

integer1 = raw_input( 'Enter First Number: \n' )
integer1 = int(integer1)
integer2 = raw_input('Enter Second Number: \n' )
integer2 = int(integer2)
add = integer1 + integer2
print ("Three answer is: "),add


Is This A Good Question/Topic? 0
  • +

Replies To: Adding integers

#2 Stutzbach  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 06-July 08

Re: Adding integers

Posted 02 July 2009 - 07:31 PM

View PostSolris, on 2 Jul, 2009 - 05:56 PM, said:

Traceback (most recent call last):
File "C:/Python31/adding.py", line 2, in <module>
integer1 = raw_input( 'Enter First Number: \n' )
NameError: name 'raw_input' is not defined

The error means that Python has not know what "raw_input" is. Python 2 had a function called "raw_input", but for Python 3 it was renamed to simply "input". Change "raw_input" to "input" and I believe your program should work.

Hope that helps and welcome to programming!
Was This Post Helpful? 0
  • +
  • -

#3 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Adding integers

Posted 03 July 2009 - 02:13 AM

Yes you have to use input instead of raw_input. One more change is needed.
Take a look at this code-
integer1 = int(input( 'Enter First Number: \n' ))
integer2 =int(input('Enter Second Number: \n' ))
add = integer1 + integer2
print ("The answer is: ",add)


This post has been edited by shadhin: 06 July 2009 - 02:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: Adding integers

Posted 05 July 2009 - 07:54 AM

That's not the best way shadhin ... because now you would want to use Error handling since it is likely you'll raise a ValueError. You really should not have "input" within any try loop to help avoid errors in your code.

You really shouldn't cast a data type in python until you understand Error handling.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1