I've got a function, displayHand() which looks like this:
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, # print all on the same line
print # print an empty line
It was provided for me (from the 600x class). As described, it takes a dict of string->ints and prints out the "hand".
What I'm having troubles with is getting this to show up properly. When I try
print('Current Hand: ', displayHand(hand))
This is what I get:
a d d m o q y
('Current Hand: ', None)
Since this was provided by the instructor, I'm sure there's a reason the code was written this way and that there's something I'm not getting.
What I'd like to get is output like this:
Current Hand: a d d m o q y
I'm absolutely brand new to this stuff so I don't even know what questions to ask.
My assessment: As far as I can piece together, displayHand() doesn't return anything and that's what's screwing it all up. But how do I catch the print output of this function and present it the way I want? I was thinking I should try to catch it in a string and have that returned, but assuming
the instructor was trying to demonstrate something, how would I do it without changing the displayHand() method?
If my assessment is off, what's going on?
Edit: This function was given to me by the class, and I'll have to use it as such. I understand changing it to return a str would be much easier, but how could I accomplish the correct output without doing that?
Further Edit: I'm dealing with an autograder that demands the output exactly as I've written. Sorry to be so fussy, I appreciate the answers and would use them if it wasn't for this.
Final Edit: Thanks for the clarification -- I'm going to use your idea and make a helper function inside this function that does what I need it to do!
FINAL final Edit: I figured it out! All I had to do was,
print('Current Hand:'),
displayHand(hand)
For real final edit:
Hah! you got it too! thank you so much for the help, I appreciate it!