为Python中的某些变量赋值的函数是运行,但不赋值

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

我正在尝试将我制作的纸牌游戏变成 Python 中的 Linux 程序。我目前正在进行的步骤是根据玩家的牌值将其转换为字符串。我设法使该步骤起作用,但它破坏了其他东西:我用来获取需要重新分配随机值的卡片列表的函数不再起作用。当程序启动时,它应该为玩家的所有六张牌分配一个值,然后打印它们的新值。但是,它会打印每张卡片的默认值。添加另一行以将其中一张卡片的值打印为数字而不是字符串显示每张卡片确实仍然具有值 1。我一直在阅读我的代码,但我无法弄清楚为什么要分配的函数卡片的随机值在以前没有做任何事情。我的代码如下:

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()

我不希望我的回答成为您代码的修复;我宁愿它解释一些你似乎还没有学过的概念,这样你就可以理解要在你的代码中改变什么。有多个问题,但我只想关注其中的几个。

如果将 int 变量放入列表中,则无法通过将新的 int 分配给列表中变量的索引来更改原始变量,因为 ints不是 mutable:

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

您也不能通过更改变量的值来更改列表中的值:

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

如果您希望变量的值影响列表的值,反之亦然,则必须为变量使用不同的类型,例如 classtuple、a list,或 dict - 可以包含值的对象(即 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]