1

I have the following function defined:

    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

Now, I want to print the following:

    Current hand: a b c

To do this, I try to do:

    print "Current hand: ", displayHand({'a':1, 'b':1, 'c':1})

And I get:

    Current hand: a b c
    None

I know that None is printed cause I am calling the print function on the displayHand(hand) function, which doesn't return anything. Is there any way to get rid of that "None" without modifying displayHand(hand)?

4 Answers 4

1

if you want to use your function in a print statement, it should return a string and not print something itself (and return None) - as you would do in a __str__ method of a class. something like:

def displayHand(hand):

    ret = ''
    for letter in hand.keys():
        for j in range(hand[letter]):
             ret += '{} '.format(letter)   # print all on the same line
    # ret += '\n'
    return ret

or even

def displayHand(hand):
     return ''.join(n*'{} '.format(k) for k,n in hand.items() )
Sign up to request clarification or add additional context in comments.

2 Comments

The lower one seems like the more "pythonic" solution here and I had thought about posting it too. Yet, the poster had initially asked for a solution without changing his original method. My answer gives him that. Other people coming here, please note that hiros answer here might be preferable for new projects.
Well, the upper one would solve my issue if I could change displayHand(). The lower one would not, cause it would not print repeated letters. See the docstring in my original post to see an example of how displayHand() should work.
0

When you trail a print with a ,, the next print will appear on the same line, so you should just call the two things on separate lines, as in:

     def printStuff():
        print "Current hand: ",
        displayHand({'a':1, 'b':1, 'c':1})

Of course you could just adapt this and create a method like:

    def printCurrentHand(hand):
        print "Current hand: ",
        displayHand(hand)

Comments

0

The only way to do this (or I believe the only way to do this) is to use return instead of print in your displayhand() function. Sorry if I didn't answer your question.

Comments

0

Your function 'displayHand' does not have to print the output, it has to return a string.

def displayHand(hand):  
    mystring='' 
    for letter in hand.keys():
        for j in range(hand[letter]):
            mystring+= letter   # concatenate all on the same line
    return mystring

BUT, you have to check the '.keys' command help as the order of the input (a/b/c) may not be respected

Comments

Your Answer

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.