Reputation: 21
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?
Upvotes: 0
Views: 90
Reputation: 1817
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.
Upvotes: 2