Reputation: 51
I am trying to work on some code for something I am starting to make but I tested a short part of something important in my project and it doesn't seem to work as I want. Here's the small segment of my code I tested:
class Fighter_Fish(object): #class to manage all fishes and their stats
fish_sum = 0
def __init__(self,fish_name, fish_type, dice_list ,skill, trigger):
self.fish_name = fish_name
self.fish_type = fish_type
self.dice_list = dice_list
self.skill = skill
self.trigger = trigger
def dice_spin(self): #commences dice spin for current turn
fish_sum = random.choice(self.dice_list)
if fish_sum == "W":
fish_sum = 0
self.skill_commence()
clown_fish_list = ["1","2","2","2","3","3"]
clown_fish = Fighter_Fish("Clown_Fish","Def",clown_fish_list,"None","None")
clown_fish.dice_spin()
print (clown_fish.fish_sum)
The print should print one of the strings in from my tested list, but it returns 0
Am I using the class functions wrong?
Upvotes: 0
Views: 2108
Reputation: 8326
In your class, you have fish_sum
defined as a class variable. When you run dice_spin
, your class creates a local variable in your scope, also called fish_sum
, but this does not refer to your class wide fish_sum
variable.
As noted by DeepSpace, you can reference that var with self
, but you might be better off not using a class wide variable, since it seems it is instance specific. To do that, instantiate it in __init__
with self
.
Upvotes: 0