Home > Enterprise >  I'm making a blackjack game, and I'm wondering why doesn't my function numbertocard u
I'm making a blackjack game, and I'm wondering why doesn't my function numbertocard u

Time:10-22

I want to use a face card value instead of the number if the card is greater then 10 but the function doesn't update the variable.

The "card" used in the function does get updated to playercard1 when I call upon it with numbertocard(playercard1), right? And if so, what exactly is the issue?

import random
def numbertocard(card):
  if card == 11:
    card == "J"
  elif card == 12:
    card == "Q"
  elif card == 13:
    card = "K"
  elif card == 14:
    card = "A"
  else:
    card = card
    return card

def cardtonumber(cardvalue):
  if cardvalue == "J":
    cardvalue == 11
  elif cardvalue == "Q":
    cardvalue == 12
  elif cardvalue == "K":
    cardvalue = 13
  else:
    cardvalue = "A"
    return cardvalue
    



def startgame():
  playercard1 = random.randint(1,14)
  numbertocard(playercard1)
  print(f"Your first card is {playercard1}")


def menu():
  play_game = input("Would you like to start a round? y/n: \n")
  if play_game == "y":
    startgame()
  else:
    print("""Say "y" """ )
    menu()



menu()

CodePudding user response:

You can achieve what you want by creating a new variable that stores the returning value of that function:

def startgame():
  playercard1 = random.randint(1,14)
  cardValue = numbertocard(playercard1)
  print(f"Your first card is {cardValue}")

or directly:

def startgame():
  playercard1 = random.randint(1,14)
  print(f"Your first card is {numbertocard(playercard1)}")

Check this out for further information of what's happening: https://www.geeksforgeeks.org/is-python-call-by-reference-or-call-by-value

  • Related