How do i change variables inside classes? in python -


i've started coding idle game put type of input in later.

my problem class stockpiles doesn't add int's def chop/mine variable "sp_wood/sp_stone", it's replacing variable number got def chop/mine. i've tried give int straight def addwood/addstone didn't work me. += should work , add 2 together. should make variable outside class , make global?

import random import time idle = true   class taskassigner:     def __init__(self, tasknum):         self.tasknum = tasknum     def choosejob(self):         if self.tasknum == 1:             self.chop()         if self.tasknum == 2:             self.mine()     def chop(self):         wood = random.randint(1, 10)         print('chopping wood')         time.sleep(1.5)         print('you got', wood)         stockpile(wood, 0)         time.sleep(0.75)     def mine(self):         stone = random.randint(1, 10)         print('mining stone')         time.sleep(1.5)         print('you got', stone)         stockpile(0, stone)         time.sleep(0.75)  class stockpile:     def __init__(self, wood, stone):         self.wood = wood         self.stone = stone         self.sp_wood = 0         self.sp_stone = 0         self.addwood(self.wood)         self.addstone(self.stone)         self.check_wood()         self.check_stone()     def addwood(self, addwood):         self.addwood = addwood         self.sp_wood += self.addwood     def addstone(self, addstone):         self.addstone = addstone         self.sp_stone += self.addstone     def check_wood(self):         print(self.sp_wood)     def check_stone(self):         print(self.sp_stone)   while idle:     taskchance = random.randint(0, 100)     if taskchance < 50:         tasknum = random.randint(0, 2)         job = taskassigner(tasknum)         job.choosejob()      else:         print('idle')         time.sleep(0.5) 

i'd change few things, because you're making harder needs be. doesn't taskassigner needs hold state, let's refactor bit function factory. let's change functions they'll work on our newly refactored stockpile class. great job encapsulating, though!!!

def taskassigner(tasknum):     """function factory tasks      taskassigner(1) --> mine     taskassigner(2) --> chop     """      def mine(stockpile):         stockpile.stone += random.randint(1,10)     def chop(stockpile):         stockpile.wood += random.randint(1,10)      tasks = [none, chop, mine]     return tasks[tasknum]  class stockpile(object):     def __init__(self, wood, stone):         self.wood = wood         self.stone = stone         # it's not clear sp_wood , sp_stone         # i'm not sure if need them! 

since taskassigner function factory instead of class, our calling signature looks little different, too.

starting_wood, starting_stone = 10, 10  # or whatever... idle = true  player_stockpile = stockpile(starting_wood, starting_stone) while idle:     if random.randint(0, 1):  # since it's coinflip, easier         task_num = random.randint(1, 2)         task = taskassigner(task_num)  # task 1 of functions         task(player_stockpile)     else:         time.sleep(0.5) 

this being said, seems should using separate modules this!

# /main.py import tasks import time  starting_stone, starting_wood = 10, 10 idle = true   class stockpile(object):     # defined above   player_stockpile = stockpile(starting_wood, starting_stone) while idle:     if random.choice((true, false)):         task = random.choice(tasks.tasklist)         task(player_stockpile)     else:         time.sleep(0.5) 

 

# /tasks.py def mine(stockpile):     stockpile.stone += random.randint(1,10)  def chop(stockpile):     stockpile.wood += random.randint(1,10)  tasklist = [mine, chop] 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -