Could anyone explain why this function gives "None" at the end?
def displayHand(hand):
"""
Displays the letters currently in the hand.
For example:
>>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})
Should print out something like:
a x x l l l e
The order of the letters is unimportant.
hand: dictionary (string -> int)
"""
for letter in hand.keys():
for j in range(hand[letter]):
print(letter,end=" ") # print all on the same line
print() # print an empty line
idjaw
26.8k10 gold badges68 silver badges84 bronze badges
2 Answers 2
The function is returning None because there is no other explicit return value; If a function has no return statement, the default is None.
Sign up to request clarification or add additional context in comments.
1 Comment
Bartek Biskupski
It does return what it should when called print(displayhand({'a':1, 'x':2, 'l':3, 'e':1}), which is "a x x l l l e" but adds None in the second line.
If a function doesn't return anything, then it automatically returns None.
answered Oct 16, 2016 at 18:16
mipadi
414k91 gold badges538 silver badges489 bronze badges
Comments
lang-py
return Noneat the end of every function, so if you don't return anything, it will returnNone