0

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
asked Oct 16, 2016 at 18:15
2
  • 1
    There's an implicit return None at the end of every function, so if you don't return anything, it will return None Commented Oct 16, 2016 at 18:16
  • Where does it give None? You aren't calling the function in this code. Commented Oct 16, 2016 at 18:20

2 Answers 2

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.

answered Oct 16, 2016 at 18:18
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

If a function doesn't return anything, then it automatically returns None.

answered Oct 16, 2016 at 18:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.