user3024763
user3024763

Reputation: 73

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 = print(input("Hello player one, what is your name: "))

print("Hello" , C1)

Upvotes: 0

Views: 823

Answers (3)

phimuemue
phimuemue

Reputation: 35983

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.

Upvotes: 0

aIKid
aIKid

Reputation: 28242

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

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1121406

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.

Upvotes: 3

Related Questions