0

I am making a simple program that repeats what I input. The current code is this:

print("Please enter your username.")
n = str(input(">> "))
print("Welcome, ",n)

However, when I run it and input, say, John, it would print the error: John is undefined, or something very similar to that. Any ideas why? Solutions?

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 3
    Possible duplicate of [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – Carcigenicate Sep 25 '17 at 13:34

1 Answers1

2

Use raw_input() instead. Using input() requires the use of "" when you enter the name and want it to be interpreted as a string.

>>> n = input(">> ")
>> "john"
>>> print n
john

When using raw_input() you can do the following:

>>> n = raw_input(">> ")
>> john
>>> print n
john

input() interprets an unquoted string input as a variable, i.e you can do something like

>>> x = 5
>>> y = input()
>> x
>>> print y
5

See also https://www.python-course.eu/input.php for further information.

PhillipD
  • 1,797
  • 1
  • 13
  • 23