Python Forum
What's your name? - returns "None"
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's your name? - returns "None"
#1
I am trying to make a simple, what is your name? hello so and so. But whenever I run it it says the name is "none" any help?

Code
name = print("Hi! What's your name? ")
print(name)
print("Hi, ", name, ".")
input("\n\nPress the enter key to exit.")
Output:
Output:
Hi! What's your name?  None Hi,  None .
Press the enter key to exit.

Thanks Guys!
Reply
#2
change
name = print("Hi! What's your name? ")
to

name = input("Hi! What's your name? ")
Reply
#3
Try this...

Will remove space from name and period as well.

name = input("Hi! What's your name? ")
print(name)
print("Hi, ", name + ".")
input("\n\nPress the enter key to exit.")
Reply
#4
To be clear, the function print() has only one purpose: to print things (i.e. the arguments passed to it) to the screen. It doesn't read any input, so doesn't have any return value, which is why the value None is assigned to name.
Reply
#5
You get None printed because after print() prints what you want it returns None. Every function returns None if you don't specify something else.
Look at this:

In [1]: def say(n):
   ...:     print(n, end=' ')
   ...:     

In [2]: l = [say(num) for num in range(1, 10)]
1 2 3 4 5 6 7 8 9 

In [3]: print(l)
[None, None, None, None, None, None, None, None, None]  # you get a list of None because say() doesn't return anything else.

In [4]: def say(n):
   ...:     print(n, end=' ')
   ...:     return n             # redifine the function. Add return satement
   ...: 

In [5]: l = [say(num) for num in range(1, 10)]  # run the same list compr.
1 2 3 4 5 6 7 8 9 

In [6]: print(l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]  # now you get a list with some data because of the return statement which returns some object. Integer in that case.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020