0

Below is a portion of my Python code for a Blackjack project. I am using the portion of code for testing because I think I finally narrowed down where the problem is but I just can't figure out how to get it work.

The problem seems to be that when I use the following syntax for iterable-unpacking of 2 returned values the code uses the function call from the iterable-unpack rather than going back to the beginning of the code so nothing is returned in the function.

The syntax is variable_1, variable_2 = function()

I tested the keep_playing() function to see if I did something wrong there but when I use it with a simple hello_world() function it works just fine.

I searched for other ways to return multiple values that I can then assign to other variables for the program to use but can't find a solution there either.

def deal():
  cards = [1,11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  player = []
  dealer = []

  while len(player) < 2:
    player.append(random.choice(cards))
    dealer.append(random.choice(cards))

  return player, dealer

#this is ITERABLE UNPACK it separates the returned values from the deal() function and assigns them to their respective variables.

player, dealer = deal()

#sum the value of the cards
player_cards = sum(player)
dealer_cards = sum(dealer)

print(f"Your cards are {player}. Total  = {player_cards}")
print(f"The dealer shows [{dealer[0]}, *]. Total = {dealer_cards}")

#the keep_playing function should start the deal function when the user types "Y" but it doesn't appear to

def keep_playing():
  play_or_quit = input("Would you like to continue playing? Type 'Y' or 'N' ").lower()
  if play_or_quit == 'y':
    deal()
  else:
    print('Thank you for playing, good luck.')
 
keep_playing()
Barry Brewer
  • 63
  • 1
  • 5
  • Please be more specific about your problem and describe what the expected behavior should be. "It doesn't work" isn't descriptive enough to help people understand your problem. See also: https://stackoverflow.com/help/how-to-ask – Roman Purgstaller Nov 07 '21 at 20:50

1 Answers1

0

The function deal() is called, but:

  • You do not deal new cards, because you only add new cards, if len(player) < 2, so during the second call nothing new happens
  • Only the deal() function is called (only the indented code is executed,but not the sum functions
  • I think you should use a gloabl while loop and asking for cards
D-E-N
  • 1,242
  • 7
  • 14
  • Thank you for so much your response! I totally forgot to use an IDE called Thonny to trouble shoot this and what you said reminded me! Whereas your response wasn't exactly accurate, there are new cards being dealt, the problem is that the cards are being held inside the keep_playing() function. I confirmed this by adding print(player,dealer) right after the deal() function is called. Now I have to figure out how to get them back in the game. Thank you! – Barry Brewer Nov 07 '21 at 11:37
  • BTW I think that you're right that a while loop is probably needed here. – Barry Brewer Nov 07 '21 at 12:05
  • Great call on the global While loop! Thank you! – Barry Brewer Nov 07 '21 at 12:38