School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,420 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




Adding integers

 

Adding integers, need help with this simple code

Solris

2 Jul, 2009 - 03:56 PM
Post #1

New D.I.C Head
*

Joined: 9 Nov, 2008
Posts: 44

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
"

CODE


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


User is offlineProfile CardPM
+Quote Post


Stutzbach

RE: Adding Integers

2 Jul, 2009 - 06:31 PM
Post #2

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 28



Thanked: 5 times
My Contributions
QUOTE(Solris @ 2 Jul, 2009 - 05:56 PM) *

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!
User is offlineProfile CardPM
+Quote Post

shadhin

RE: Adding Integers

3 Jul, 2009 - 01:13 AM
Post #3

D.I.C Regular
Group Icon

Joined: 16 Sep, 2008
Posts: 421



Thanked: 19 times
Dream Kudos: 3650
My Contributions
Yes you have to use input instead of raw_input. One more change is needed.
Take a look at this code-
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: 6 Jul, 2009 - 01:51 AM
User is offlineProfile CardPM
+Quote Post

code_m

RE: Adding Integers

5 Jul, 2009 - 06:54 AM
Post #4

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:03AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month