0

I am trying to make function pc_moves() to return sum(computers_cards) but when 4th value is added, it ignores 4th value.

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
your_cards = []
computers_cards = []
game = True 
pc_sum = 0


def f_wants_to_play():
  global game
  wants_to_play = input("Do you want to play a game of Blackjack? Type 'y' or 'n':")
  if wants_to_play == "y":
    print("blackjack")
  elif wants_to_play == "n":
    print("You don't wanna play! Goodbye!")
    game = False
  else:
    print("Type 'y' or 'n'!")
    f_wants_to_play() 
    
def pc_moves():
  global pc_sum
  global computers_cards
  computers_cards = random.sample(cards, 2)
  pc_sum = sum(computers_cards)
  print(f"computers cards: {computers_cards}")
  def add_new_card_pc(pc_sum, computers_cards):
    if pc_sum == 22:
      return pc_sum
    elif pc_sum < 17:
      computers_cards.append(random.choice(cards))
      pc_sum = sum(computers_cards)
      if pc_sum == 21:
        return pc_sum
      elif pc_sum > 21:
        if 11 in computers_cards:
          place_of_ace = computers_cards.index(11)
          computers_cards[place_of_ace] = 1
          pc_sum = sum(computers_cards)
          if pc_sum > 21:
            return pc_sum
          else:
            add_new_card_pc(pc_sum, computers_cards)
        else:
          return pc_sum
      elif 17<pc_sum<21:
        return pc_sum
      else:
        add_new_card_pc(pc_sum, computers_cards)
    return pc_sum
  pc_sum = add_new_card_pc(pc_sum, computers_cards)

def blackjack():  
  while game == True:
    f_wants_to_play()
    if not game:
      break
    your_cards = random.sample(cards, 2)
    print(f"Your cards: {your_cards}, current score: {your_cards[0]+your_cards[1]}")
    user_sum = sum(your_cards)
    pc_moves()
    print(f"pc sum : {pc_sum} computers_cards: {computers_cards}")
    
blackjack()

It is ignoring 4th value. For example result:

Do you want to play a game of Blackjack? Type 'y' or 'n':y

blackjack

Your cards: [10, 10], current score: 20

computers cards: [4, 11]

pc sum : 15 computers_cards: [4, 1, 10, 10]

OCa
  • 298
  • 2
  • 13
  • Welcome to SO! Boiling your code down to a minimal reproducible example would probably help us help you. See [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/12846804) – OCa Aug 14 '23 at 22:06
  • Are you nesting `def` functions inside other `def` and inside your code? That, plus declaring variables global, is maybe not helping. Consider improving readability for you and for us. – OCa Aug 14 '23 at 22:17
  • Is it because pc_sum is not global inside `add_new_card_pc` – Joe Aug 14 '23 at 22:36
  • 1
    Why even have a `pc_sum` variable? Why not just have a list of cards, that is mutable, and sum it each time you need a comparison? – Joe Aug 14 '23 at 22:38
  • Read [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. These are the exact techniques I would use to figure out what is wrong with your code. – Code-Apprentice Aug 14 '23 at 23:29

1 Answers1

0

wherever you call add_new_card_pc(pc_sum, computers_cards) recursivly inside itself, you must return the output. what happening is that you don't return the output from last iteration.

simply just replace add_new_card_pc(pc_sum, computers_cards) with

return add_new_card_pc(pc_sum, computers_cards)

inside add_new_card_pc() method.

so your code will look like this:

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
your_cards = []
computers_cards = []
game = True 
pc_sum = 0


def f_wants_to_play():
  global game
  wants_to_play = input("Do you want to play a game of Blackjack? Type 'y' or 'n':")
  if wants_to_play == "y":
    print("blackjack")
  elif wants_to_play == "n":
    print("You don't wanna play! Goodbye!")
    game = False
  else:
    print("Type 'y' or 'n'!")
    f_wants_to_play() 
    
def pc_moves():
  global pc_sum
  global computers_cards
  computers_cards = random.sample(cards, 2)
  pc_sum = sum(computers_cards)
  print(f"computers cards: {computers_cards}")
  
  def add_new_card_pc(pc_sum, computers_cards):
    if pc_sum == 22:
      return pc_sum
    elif pc_sum < 17:
      computers_cards.append(random.choice(cards))
      pc_sum = sum(computers_cards)
      if pc_sum == 21:
        return pc_sum
      elif pc_sum > 21:
        if 11 in computers_cards:
          place_of_ace = computers_cards.index(11)
          computers_cards[place_of_ace] = 1
          pc_sum = sum(computers_cards)
          if pc_sum > 21:
            return pc_sum
          else:
            return add_new_card_pc(pc_sum, computers_cards) #changed this line
        else:
          return pc_sum
      elif 17<pc_sum<21:
        return pc_sum
      else:
        return add_new_card_pc(pc_sum, computers_cards) # changed this line
    return pc_sum
  
  pc_sum = add_new_card_pc(pc_sum, computers_cards)

def blackjack():  
  while game == True:
    f_wants_to_play()
    if not game:
      break
    your_cards = random.sample(cards, 2)
    print(f"Your cards: {your_cards}, current score: {your_cards[0]+your_cards[1]}")
    user_sum = sum(your_cards)
    pc_moves()
    print(f"pc sum : {pc_sum} computers_cards: {computers_cards}")
    
blackjack()

for better understanding, consider this simple example where we calling the function recursively but ignoring the return value:

def fibo(a, b, n):
    c = a + b
    print(f"c is: {c}")

    if c >= n:
        return c
    else:
        fibo(b, c, n)
    return c

print(f"return value is: {fibo(2, 3, 50)}")

the output of this code will be:

c is: 5
c is: 8
c is: 13
c is: 21
c is: 34
c is: 55
return value is: 5
beh-f
  • 103
  • 1
  • 9
  • now code works but there are still things that i don't understand. Firstly before returning the output, i think it should've printed pc_sum as total of first 2 integers, the initial values of 'computers_cards' but it adds third integer too. Why? Secondly in the fibo() method, as my understanding after 'if c >= n:' it returns c then it goes to next code which is another 'return c' why code doesn't crash repeating same lines of code? Another thing if i delete 'return c' code after if else statements it prints None but it shouldn't be None because in if statement we just returned c? – MaximusPrima Aug 15 '23 at 18:16
  • @MaximusPrima the fibo() function is called 6 times. we called it first time and it called itself 5 more times. the first call doesn't finish running and doesn't return anything until all of the other 5 calls are finished running. the last call calculates c as 55 and returns it to 5'th call but 5'th call ignores it and returns it's own c which is 34 to the 4'th call, 4'th call ignores it and returns it's own c which is 21 to the 3'th call, 3'th call ..., 2nd call ignores it and returns it's own c which is 8 to the first call, and first call ignores it and returns it's own c which is 5. – beh-f Aug 15 '23 at 22:28
  • @MaximusPrima in order to our function work as expected we have to add `return` to fibo() call in the else statement. – beh-f Aug 15 '23 at 22:33