1

I'm working through the edx python course. I've done everything correctly in the last assignment. However, I'm struggling with this print issue.

I have a function displayHand(hand) which essentially takes a dictionary with keys being letters and values be the number of occurrences of the letters. The function then prints a string with each key appearing the same number of times as its value. So for example if

hand={a:3, b:4, c:1}
displayHand(hand)=a a a b b b b c

Now in the program for any given hand it wants me to display it using the displayHand function with the text "current hand: "

Again, if hand={a:3, b:4, c:1} the program should display

current hand: a a a b b b b c

How do I do this with a print statement? I've tried print("current hand"+str(displayHand(hand)), but this first evaluates the function and then just prints None. I can't put print("Current Hand: ") and displayHand(hand) directly underneath, because then they print on different lines. I need to get them to print on the same line.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Pavan Sangha
  • 249
  • 1
  • 3
  • 10
  • Which version of Python are you using? – khelwood Feb 22 '17 at 17:02
  • presumably your `displayHand` function ends with some kind of `print .....` statement? Try ending it with a `return.....` statement instead – asongtoruin Feb 22 '17 at 17:03
  • It is assessed on the edx platform so i'm guessing they maybe running python 3.5. Unfortunately i cannot put return in the function because it was a function they provided for you. I'm guessing it's meant to be doable with a just a print statement? – Pavan Sangha Feb 22 '17 at 17:38
  • Is there still something missing/not working? If one of the answers provided works for you, please accept it by clicking on the green check next to it to show others that it solved your problem. :) – Cleb Mar 07 '17 at 13:10

4 Answers4

4

Python 3:

def displayHand(dct):
    for key, val in sorted(dct.items()):  # sorted if key order should be abc
        print((key + " ") * val, end="")


hand = {"a": 3, "b": 4, "c": 1}

print("current hand: ", end="")
displayHand(hand)

Notice the end="" which appends whatever is inside "". The default setting is end="\n". In this case, we will just use an empty string. See here and here.

Output:

current hand: a a a b b b b c 
Spherical Cowboy
  • 565
  • 6
  • 14
  • I would not use `dict` as a variable name as it is a reserved name. You could also loop through the dictionary using `key, item` which would avoid the `dict[key]`. – Cleb Feb 22 '17 at 17:23
  • Ah, thanks, should have been dct. I am usually a lst and dct kind of guy. – Spherical Cowboy Feb 22 '17 at 17:29
1

That should work for you :

print("current hand: ", end="")
displayHand(hand)

In print() function default end is new line. In the code above you just change it to whatever you want.

Symonen
  • 628
  • 2
  • 7
  • 19
0

You can use print twice, you just have to add a comma after the first print ex: print("a"), print("b").

Ali Camilletti
  • 116
  • 1
  • 3
  • 11
0

You could do the following:

def displayHand(hand):
    print('{} {}'.format('current hand:', ' '.join([' '.join([li] * ni) for li, ni in sorted(hand.items())])))

That creates a string first which is then printed.

Then

displayHand(hand)

prints the desired output:

current hand: a a a b b b b c
Cleb
  • 25,102
  • 20
  • 116
  • 151