-3

I'm not familiar with functions but somehow got the scenario 1 to work and print out a set of 5 random cards like so: A 3 J 2 8. In 2 however I'm trying to use the function poker_hand in poker_game so as to get something like:

Player 1: # # # # #,
Player 2: # # # # #, 
etc. 

But instead the output looks like:

5 6 10A 5 Player 2 <function poker_hand at 0x105818400>
K 7 A 2 6 Player 3 <function poker_hand at 0x105818400>
Q 104 3 3 Player 4 <function poker_hand at 0x105818400>
8 102 K 7 Player 5 <function poker_hand at 0x105818400>
5 2 A 6 2 Player 6 <function poker_hand at 0x105818400>
Q A 5 9 6 Player 7 <function poker_hand at 0x105818400>
A A 8 3 9 Player 8 <function poker_hand at 0x105818400>
K 8 3 J K Player 9 <function poker_hand at 0x105818400>

how could this be fixed?

cards = ['A ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', '10', 'K 
', 'Q ', 'J ']

def poker_hand(x):
    for i in range(0, 5):
        pick = random.choice(cards)
        print(pick, end='')
    return poker_hand

poker_hand(cards)

#

def poker_game(num_players):
    for i in range(2, 10): 
        print("Player {}:".format(i), poker_hand(cards))
    return poker_game
poker_game(cards)
le-dumbass
  • 25
  • 1
  • 1
  • 3

1 Answers1

-1

I think you are very close. I have made a few small edits:

import random
cards = ['A ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', '10 ', 'K ', 'Q ', 'J ']

def poker_hand(x):
    hand = []
    for i in range(0, 5):
        pick = random.choice(cards)
        hand.append(pick)
    return hand

def poker_game(num_players):
    for i in range(2, 10): 
        print("Player {}:".format(i), "".join(poker_hand(cards)))
poker_game(cards)

This results in:

Player 2: 3 9 K 7 2 
Player 3: 8 2 J 7 8 
Player 4: 7 10 3 3 3 
Player 5: 5 J J A 2 
Player 6: 10 6 6 J 10 
Player 7: K 8 6 Q J 
Player 8: 9 Q 3 4 K 
Player 9: 10 8 8 7 Q 
briancaffey
  • 2,339
  • 6
  • 34
  • 62
  • yes, thats what I had in mind thank you. `"".join` adds a space and joins them together? – le-dumbass Feb 09 '18 at 01:58
  • @le-dumbass yes, `"".join(x)` takes the elements of a list and prints them "right next to eachother. In this case you have added spaces after the card names. If you didn't have the spaces, you would do `" ".join(x)` to get something like `10 8 8 7 Q`. – briancaffey Feb 09 '18 at 02:00
  • @le-dumbass could you mark the answer if correct if it is what you need? thanks!! – briancaffey Feb 09 '18 at 02:10