Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBleza Takouda
5,129 Pointsinput() vs raw_input()
I encountered a NameError when using the input function [item=input("> ")]; but it was solved when I tried the raw_input(">"). Could explain briefly the difference between both functions. Thank you .
2 Answers
William Li
Courses Plus Student 26,868 PointsShort answer is that raw_input()
in Python version 2.x was replaced with input()
in Python 3.x.
The instructor is using Python 3.x for the course, my guess is that you're running Python 2.x on your own machine.
Alexander Davison
65,469 Pointsraw_input
only exists in Python 2. raw_input
in Python 2 is the same thing as input
in Python 3.
However, if you try to use input
in Python 2, for instance let's say I was inputting my name into a program (and I used the input
function instead of the raw_input
function):
What's your name? Alex
This will cause an error because Python 2 will start looking for a variable called Alex
.
So, that means you could evaluate things for input
(in Python 2 only):
What's your age? 15 + 16
And Python will get the input as "31".
raw_input
automatically converts the input into a string, so it doesn't do all the parsing.
So, if you wanted to input "Alex" without referring to the Alex
variable, you would have to put parentheses around it:
What's your name? "Alex"
But, in Python 3, to avoid confusion, Python changed the input
to automatically convert the input into a string and removed the raw_input
function.
However, if you want to parse the input you still can do this:
parsed_input = eval(input("What's your name? "))
I hope you understand. :)
Good luck! ~alex
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsI provided the long answer to avoid confusion :)