Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:40:30+00:00 2026-06-08T22:40:30+00:00

Python is my first language, and I am very new to it, so the

  • 0

Python is my first language, and I am very new to it, so the answer may be very clear, but after many hours looking and experimenting, I am not sure what is causing the problem.

An overview of the module:
The DicePool module is meant to manage collections of “dice”, stored as dictionary items. Each dictionary key (herein poolKey) has a list containing information about one “type” of dice, most importantly, a tuple describing its faces and an integer representing the quantity of “dice” type ‘x’ in the “pool.”

My specific question regards the Transfer method, which used to be two methods (send and receive, basically), but which I thought I could combine into one method. When the test code at the bottom runs, I’d like it to leave dp.dictPool[poolKey][1] == 0 and dp2.dictPool[poolKey][1] == 2. But in every attempt I’ve made, the values come out the same. Sorry I can’t categorize this question better . . . I don’t really know what the problem is.

Anyway, one half of the Transfer method is supposed to run for the “sender” instance, and one half is supposed to run for the “receiver” instance.

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:40:32+00:00Added an answer on June 8, 2026 at 10:40 pm

    The problem is in this line:

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

    The values in your dictPools are lists. Here, you set one object’s dictPool value to the same list as in the other one. Not a copy of the list, but the same list. So later if you add or subtract from that list, it will affect the other as well, because they are sharing one list object.

    Try doing self.dictPool[poolKey] = senderPool.dictPool[poolKey][:]. The [:] grabs the contents of the list rather than the list object iself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to programming and am learning Python as my first language. I
I am trying to write my very first python script. This was working but
I'm using Django (and Python) for the first time, and I'm looking for the
I'm fairly new to actual programming languages, and Python is my first one. I
I learned Python as my first serious (non BASIC) language about 10 years ago.
I'm a self-taught programmer who jumped into Python as my first language about 7-8
I am currently learning c++.My first language was python.I am used to coding in
I can't find the answer on SO but it's very likely that the argument
First off, I'm looking to write GUI apps based on an interpreted language (PHP
The first language I learnt was PHP, but I have more recently picked up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.