functions within functions card game
Question:
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)
Answers:
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
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)
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