Don't understand "raw_input"-function. It won't work

This is the place for queries that don't fit in any of the other categories.

Don't understand "raw_input"-function. It won't work

Postby crazzyjoel » Fri Aug 23, 2013 9:19 am

Hello everyone, i have an issue with the "raw_input"fuction and i can't seem to figure it out. :(

Whenever i'm trying to execute a code with raw_input, i always get something called a "builtins.NameError", like this:

Code: Select all
Traceback (most recent call last):
  File "c:\Users\Lars\Desktop\More_Control_Flow_Tools.py", line 1, in <module>
builtins.NameError: name 'raw_input' is not defined


And the code i'm trying to execute is:

Code: Select all
x = int(raw_input("Please enter an integer: "))
if x < 0:
     x = 0
     print('Negative changed to zero')
elif x == 0:
     print('Zero')
elif x == 1:
     print('Single')
else:
     print('More')


It's a code directly taken from this site, http://docs.python.org/2/tutorial/controlflow.html :)

The only thing i've added are the "parenthesis" (because i think you need them and i don't understand why they don't use any paranthesis) which belongs to the print-syntax. Nothing more. I've also tried to not to use the paranthesis but with no good results. Does someone know what i should do? Are there any other sort of code needed if you want to use the raw_input-function? Thank you for your time :D, i really hope you can help me out with this issue, i really love programming and i want to learn as much as possible :)

Regards Joel
crazzyjoel
 
Posts: 4
Joined: Tue Aug 13, 2013 10:03 am

Re: Don't understand "raw_input"-function. It won't work

Postby Kebap » Fri Aug 23, 2013 9:58 am

Hey Joel, welcome to python!

Thing is, you are probably reading a tutorial for python 2.x but have installed a version of python 3.x

Now there are not many differences between those, but you already found some very subtle ones:

* python 3 needs paranthesis for print()
* python 2's raw_input is now called input()

Solution is simple: Change tutorials or install a 2.x version, which I would maybe recommend for now.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Don't understand "raw_input"-function. It won't work

Postby crazzyjoel » Fri Aug 23, 2013 10:29 am

Kebap wrote:Hey Joel, welcome to python!

Thing is, you are probably reading a tutorial for python 2.x but have installed a version of python 3.x

Now there are not many differences between those, but you already found some very subtle ones:

* python 3 needs paranthesis for print()
* python 2's raw_input is now called input()

Solution is simple: Change tutorials or install a 2.x version, which I would maybe recommend for now.


Oh, haha. Thank you very much :D Didn't knew that. Then i know the reason why raw_input didn't work. Once again, thank you :)
crazzyjoel
 
Posts: 4
Joined: Tue Aug 13, 2013 10:03 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests