Topic: Input with Python

Hello everyone,

I'm brand new to python, but i have used java, c++, and c#. I'm working through some tutorials and im coming across the following problem:

When i try to get user input, i expect the following

name = input("Please enter your name: ")
>>>Greg
print("Hello", name)
>>>Hello, Greg

instead what i get is this:

Please enter your name: Greg

Traceback (most recent call last):
  File "C:...py", line 1, in <module>
    name = input("Please enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'Greg' is not defined

I have determined that when i use quotes around the input, the program executes succesfully, ie. entering 'Greg' in stead of Greg. Is this how python input works ? or am i just making a dumb syntax error i'm not recognizing ?

Thumbs up

Re: Input with Python

Ok, first of all you don't need parentheses around the print statement, just quotes.

Second, a more efficient way to have it print the name in a sentance, is to use a placeholder (%s or %r) in this case, so it would look like this"

print "Hello %r" % name

Third, try using raw_input instead of just input.

This worked for me:

name = raw_input("Enter name: ")
print "Hello %r" % name

Thumbs up