0

Python 是我的第一语言,我对它很陌生,所以答案可能很清楚,但经过数小时的寻找和试验,我不确定是什么导致了问题。

模块概述:DicePool 模块用于管理“骰子”集合,存储为字典项。每个字典键(此处为 poolKey)都有一个列表,其中包含有关一种“类型”骰子的信息,最重要的是,一个描述其面的元组和一个表示“池”中“骰子”类型“x”的数量的整数。

我的具体问题是关于 Transfer 方法,它曾经是两种方法(基本上是发送和接收),但我认为我可以将它们组合成一种方法。当底部的测试代码运行时,我希望它离开 dp.dictPool[poolKey][1] == 0 和 dp2.dictPool[poolKey][1] == 2。但是在我所做的每一次尝试中, 值是一样的。抱歉,我无法更好地对这个问题进行分类。. . 我真的不知道问题是什么。

无论如何,Transfer 方法的一半应该为“发送者”实例运行,而另一半应该为“接收者”实例运行。

import random

class DicePool(object):

    def __init__(self):
        self.dictPool = {}

    def AddDice(self, poolKey, faces = 6, quant = 1, color = "white"):
        '''faces must be int or items 'a,b,c'; count must be int or def to 1'''
        try: #if count is not an integer, it defaults to 1
            quant = int(quant)
        except:
            print("Quant is not an integer, defaulting to 1")
            quant = 1
        try: #if faces is can be int, a list is built of numbers 1 to faces
            faces = int(faces)
            if faces < 2: #a 1 or 0-sided die breaks the program
                faces = 2
            tempList = []
            for i in range(1, faces+1):
                tempList.append(i)
            faces = tempList
        except: #if faces is not an integer, it is split into list items by ","
            faces = faces.split(",")
        if poolKey in self.dictPool.keys(): #if the key already exists in pool
            self.dictPool[poolKey][1] += quant #add to the quantity, 
        else: #if the key does not already exist, set all attributes
            self.dictPool[poolKey] = [faces, quant, color]

    def Transfer(self, poolKey, targetPool, sendQuant, senderPool = None):
        '''targetPool must be DicePool instance'''
        if targetPool:
            self.dictPool[poolKey][1] -= sendQuant
            targetPool.Transfer(poolKey, None, sendQuant, self)
        else:
            try:
                self.dictPool[poolKey][1] -= sendQuant
            except:
                self.dictPool[poolKey] = senderPool.dictPool[poolKey]
                self.dictPool[poolKey][1] = sendQuant

dp = DicePool()
dp2 = DicePool()

dp.AddDice("d6")
dp.AddDice("d6")
dp.Transfer("d6",dp2,2)
print(dp.dictPool,dp2.dictPool)
4

4 回答 4

2

问题出在这一行:

self.dictPool[poolKey] = senderPool.dictPool[poolKey]

dictPools 中的值是列表。在这里,您将一个对象的 dictPool 值设置为与另一个对象相同的列表。不是列表的副本,而是相同的列表。因此,稍后如果您从该列表中添加或减去,它也会影响另一个,因为它们共享一个列表对象。

尝试做self.dictPool[poolKey] = senderPool.dictPool[poolKey][:]。抓取列表的[:]内容而不是列表对象本身。

于 2012-08-02T03:54:10.727 回答
0

在 if/else 的两个部分中,Transfer您都在减去数量。您不想在一种情况下减去它,但在另一种情况下添加它吗?

于 2012-08-02T03:51:32.633 回答
0

else: try: self.dictPool[poolKey][1] -= sendQuant应该是+= sendQuant代替吗?

可能是语义问题,而不是 Python 语法问题。

于 2012-08-02T03:51:57.210 回答
0

这是 Transfer() 方法的一个版本,它似乎按预期工作(或多或少),尽管它看起来很笨拙:

def Transfer(self, poolKey, targetPool, sendQuant, senderPool = None):
    '''targetPool must be DicePool instance'''
    if isinstance(targetPool, DicePool):
        #valid target, so subtract dice here, then call Transfer on target
        temp = self.dictPool[poolKey][1:2][0] - sendQuant
        print("sent",sendQuant,"leaving",temp)
        targetPool.Transfer(poolKey, "Receiver", sendQuant, self)
    elif targetPool == "Receiver":
        #this executes if a DicePool is named as the targetPool of Transfer
        if poolKey in self.dictPool.keys():
            self.dictPool[poolKey][1:2][0] += sendQuant
        else:
            self.dictPool[poolKey] = senderPool.dictPool[poolKey]
            self.dictPool[poolKey][1:2] = [sendQuant]
        print("now have",self.dictPool[poolKey][1:2][0])
        pass
    else:
        print("Not a valid targetPool, apparently")
于 2012-08-03T20:12:04.760 回答