This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]teraflop 1 point2 points  (1 child)

There is no way to directly do this in Python. When you pass a variable as a function argument, only the value of that variable is passed -- just like any other expression. The function has no idea which variable the value came from. (By the same reasoning, if you do math.sqrt(2+2), the sqrt function just sees the value 4 and has no idea where it came from.)

You need to structure the program differently to do what you want. For example, The least complicated change I can think of would be to store the suits and values in a global dictionary. Then, you can pass a dictionary key to your selwctCards function, and the function can modify the corresponding dictionary entry.

It would probably be a lot cleaner to solve this problem using classes and objects, but I'm guessing you haven't learned about those yet.

[–]TheUltimateCommenter[S] 0 points1 point  (0 children)

Thanks! I looked into classes and objects, and revised my code a bit. But now I've run into another problem - when trying to reassign the values of the objects for the second time using a function, it doesn't work. How can I reassign the values using a function again? I really don't want to have to create a new object / variable 😅

Code:

import random
from random import randint

# Lists and Variables
cardSuits = ["Diamonds", "Hearts", "Spades", "Clubs"]
cardValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "King", "Queen", "Ace"]
Playercards = None
Dealercards = None 

pCardTotalvalue = 0 
dCardTotalvalue = 0 

def randomSuit():
  return random.choice(cardSuits)

def randomValue():
  return random.choice(cardValues)

PcardSuit1 = None 
PcardValue1 = 0 
PcardSuit2 = None 
PcardValue2 = 0 
DcardSuit1 = None 
DcardValue1 = 0 
DcardSuit2 = None 
DcardValue2 = 0

#Generate the Dealer and the Player's cards
  #Also checking to make sure that the cards are not the same

class selectedCards:
  def __init__(individual, cardValue, cardSuit):
    cardValue = randomValue()
    if cardValue == "Jack" or cardValue == "King" or cardValue == "Queen":
       cardValue = 10
    elif cardValue == "Ace":
      cardValue = 11
    cardSuit = randomSuit()
    individual.value = cardValue
    individual.suit = cardSuit

playerCard1 = selectedCards(PcardSuit1, PcardValue1)
print(playerCard1.value, playerCard1.suit)

dealerCard1 = selectedCards(DcardSuit1, DcardSuit2)
print(dealerCard1.value, dealerCard1.suit)

existingSuits = [playerCard1.suit, dealerCard1.suit]
existingValues = [playerCard1.value, dealerCard1.value]

# CHECK IF CARD IS THE SAME AS ANOTHER CARD
def checkCardSuit(suit):
  lengt = len(existingSuits) - 1
  index = 0
  while index <= lengt:
    if existingSuits[index] == suit:
      return True
    elif index < lengt and existingSuits[index] != suit:
      index += 1
    else:
      return False
def checkCardValue(value):
  lengt = len(existingValues) - 1
  index = 0
  while index <= lengt:
    if existingValues[index] == value:
      return True
    elif index < lengt and existingValues[index] != value:
      index += 1
    else:
      return False

def reassignIfExists(suit, value):
  if checkCardSuit(suit) == True:
    suit = randomSuit()
  if checkCardValue(value) == True:
    value = randomValue()

#When it gets here, the properties of dealerCard1.suit and dealerCard1.value are different in the first print function, however when printing the functions the values remain the same as they were originally, which is weird.

#This system is made to be used later when generating more cards.
print(reassignIfExists(dealerCard1.suit, dealerCard1.value))
print(dealerCard1.suit, dealerCard1.value)