i posted this a while ago but i've cleaned it a bit and since then, the problem is still here because no one really gave a right answer.
so what my problem is, is that my varibles that were defined outside the class and loop stonepile
and woodpile
(that are inside a class) aren't adding stone
/wood
(which are also inside the same class) to itself multiple times, i know this because i get the stonepile
/woodpile
to be printed. i have tested that the problem is actually the stonepile
/woodpile
at the begining being reset every time it is told to add stone
/wood
to stonepile
/woodpile
. i know this because i did this with them:
y = random.randint(1,5)
x = random.randint(1,5)
woodpile = y
stonepile = x
and the results were that if the stone mined was 1 and the randint for x was 5, it would print 6. or something similar. So is there any way this can be fixed please?
the whole code here:
import random
import time
idle = True
woodpile = 0
stonepile = 0
while True:
class Taskassigner:
def __init__(self,tasknum,stonepile,woodpile):
self.tasknum = tasknum
self.woodpile = woodpile
self.stonepile = stonepile
def choosejob(self,stonepile,woodpile):
if self.tasknum == 1:
self.chop(woodpile)
if self.tasknum == 2:
self.mine(stonepile)
def chop(self,woodpile):
wood = random.randint(1, 10)
print('chopping wood')
time.sleep(1.5)
print('you got', wood)
woodpile += wood
print(woodpile)
time.sleep(0.75)
def mine(self,stonepile):
stone = random.randint(1, 10)
print('mining for stone')
time.sleep(1.5)
print('you got', stone)
stonepile += stone
print(stonepile)
time.sleep(0.75)
while idle:
taskchance = random.randint(0,1)
if taskchance == 1:
tasknum = random.randint(0,2)
job = Taskassigner(tasknum,stonepile,woodpile)
job.choosejob(stonepile,woodpile)
print
else:
print('idle')
time.sleep(0.5)