Skip to content Skip to sidebar Skip to footer

Why Does Getting The User Input Return None?

When I execute this python code, the answer gives me 'Hello none' and doesn't give me a specific name. Ex. if my name was bob, I want it to say 'Hello Bob' not 'Hello none' C1 = pr

Solution 1:

The print() function returns None. Remove the print() call, put that on a separate line instead:

C1 = input('Hello player one, what is your name: ')
print(C1)
print("Hello" , C1)

or leave it out altogether. The input() function itself already prints the question for you.

Solution 2:

print returns None. Remove it and you'll be fine:

C1 = input("Hello player one, what is your name: ")

Basically, you're assigning the value of print(something) to C1, and since print doesn't return anything, you're putting the value of None to C1. Hence print('Hello', C1) will result in Hello None.

Demo:

>>>C1 = input("Hello player one, what is your name: ")
Hello player one, what is your name: Bob
>>>print("Hello", C1)
Hello Bob

Solution 3:

Your code can be re-written as follows:

tmp = input("Hello player one, what is your name: ")
C1 = print(tmp)

print("Hello" , C1)
print("Hello", tmp) # that's what you probably want

The result you get from the input call is stored in tmp, the result of the print call is stored in C1. I assume that you'd like to obtain the input given by the user (i.e. the result of input). The result of print in python is simply None.

Post a Comment for "Why Does Getting The User Input Return None?"