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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.