Home > Net >  A function to assign values to some variables in Python is running, but not assigning the values
A function to assign values to some variables in Python is running, but not assigning the values

Time:02-04

I'm trying to turn a card game I made into a Linux program in Python. The step I'm currently on is to convert the player's cards into strings based on their values. I managed to get that step working, but it broke something else: The function I used to take a list of the cards that need to be reassigned random values no longer works. When the program starts, it's supposed to assign a value to all six of the player's cards and then print their new values. However, it instead prints the default value for each card. Adding another line to print one of the cards' values as a number rather than a string shows that each card indeed still has a value of 1. I keep reading over my code, but I'm failing to figure out why the function to assign random values to the cards isn't doing anything when it did before. My code is as follows:

import sys
from os import system
from time import sleep
import random

# Variable Definitions
choosePlayer = input('Choose your player:\n[1] The Cowboy\n[2] The Hustler\n[3] The Sheriff\n[4] The Outlaw\n[5] The Barkeep\n[6] The Quarter Horse\n\nYour choice [1-6]: ')
pCard1 = 1
pCard2 = 1
pCard3 = 1
pCard4 = 1
pCard5 = 1
pCard6 = 1
playerCards = [pCard1,pCard2,pCard3,pCard4,pCard5,pCard6]
emptyVar = None
spentCards = [pCard1,pCard2,pCard3,pCard4,pCard5,pCard6]
# Function Definitions
def clear():
    system('clear')
def catPlayerToStr(playerChoice):
    theCat = {
        1: "Cowboy",
        2: "Hustler",
        3: "Sheriff",
        4: "Outlaw",
        5: "Barkeep",
        6: "Quarter Horse",
    }
    return theCat.get(playerChoice,"Failure to determine player")
playerChoice = int(choosePlayer)
def generateCards():
    for i in range (len(spentCards)):
        spentCards[i] = random.randint(1, 13)

def catCardToStr(playerCards):
    for i in range (6):
        theOtherCat = {
                1: "Ace",
                2: "2",
                3: "3",
                4: "4",
                5: "5",
                6: "6",
                7: "7",
                8: "8",
                9: "9",
                10: "10",
                11: "Jack",
                12: "Queen",
                13: "King",
    }
    return theOtherCat.get(playerCards,"Failure to determine card")

def listCards():
    for i in range (len(playerCards)):
        currentCard = catCardToStr(playerCards[i])
        print(currentCard   "\n")
# Startup
clear()
print("You are the "   catPlayerToStr(playerChoice)   ". Welcome to Six-Chance Trade. Starting game in:\n3")
sleep(1)
print("2")
sleep(1)
print("1")
sleep(1)
system('clear')
print("The "   catPlayerToStr(playerChoice)   "'s cards:")
generateCards()
listCards()

CodePudding user response:

You are assigning random values to spentCards instead of playerCards.

def generateCards():
    for i in range (len(playerCards)):
        playerCards[i] = random.randint(1, 13)

CodePudding user response:

I don't want my answer to be a fix for your code; I'd rather it explain some concepts that it seems like you haven't learned yet, so that you can understand what to change in your code. There are multiple issues, but I want to focus on just a couple.

If you put int variables in a list, you can't change the original variables by assigning a new int to a variable's index in the list, because ints are not mutable:

>>> foo = 1
>>> bar = 2
>>> blah = [foo, bar]
>>> print(blah)
[1, 2]
>>> blah[0] = 7
>>> print(blah)
[7, 2]
>>> print(foo)
1

You also can't change the values in the list by changing the variables' values:

>>> lst = [foo, bar]
>>> print(lst)
[1, 2]
>>> foo = 6
>>> print(lst)
[1, 2]

If you want the variables' values to affect the list's values, and vice versa, you would have to use a different type for the variables, like a class, a tuple, a list, or a dict - something that is an object that can contain values (i.e. is mutable):

class Card:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return str(self.value)  # so it prints its value when the object is printed


cards = [Card(i   1) for i in range(6)]
print(cards)
# output: [1, 2, 3, 4, 5, 6]

cards[0].value = 12
print(cards)
# output: [12, 2, 3, 4, 5, 6]

card1 = Card(1)
card2 = Card(2)
variable_cards = [card1, card2]
print(variable_cards)
# output: [1, 2]

card1.value = 30
print(variable_cards)
# output: [30, 2]
  •  Tags:  
  • Related