I made a Class, and I am trying to change a variable (a variable that I set up at the beggining of the program) from inside a Class. But an error pops up and says:

"local variable 'widget_stock_price' referenced before assignment"

I made the variable "widget_stock_price" at the beggining of the program and when I try to change it from inside a Class it doesnt let me. So, whats the problem, and what can I do to change the variable from inside a class?

thanks!

here is the program:
___________________________________________________________________________
wind_stock_price = 50
empire_stock_price = 50
widget_stock_price = 50

player_name = raw_input("What is your name?")

class Stock_investor(object):

def __init__(self, num_wind, num_empire, num_widget, name):
self.num_wind = num_wind
self.num_empire = num_empire
self.num_widget = num_widget
self.name = name

def print_stock(self):
print self.name, "has", self.num_wind, "stocks in wind"
print self.name, "has", self.num_empire, "stocks in empire"
print self.name, "has", self.num_widget, "stocks in widget\n"

def print_name(self):
print self.name

def say_hello(self):
print "Hello, my name is", self.name

def buy_widget(self, amount):
self.num_widget += amount
widget_stock_price += amount

def buy_empire(self, amount):
self.num_empire += amount

def buy_wind(self, amount):
self.num_wind += amount

def sell_widget(self, amount):
self.num_widget -= amount

def sell_wind(self, amount):
self.num_wind -= amount

def sell_widget(self, amount):
self.num_empire -= amount

bob = Stock_investor(5, 5, 10, "Bob")
joe = Stock_investor(5, 5, 7, "Joe")
thor = Stock_investor(5, 5, 5, "Thor")

bob.say_hello()
joe.say_hello()
thor.say_hello()

print widget_stock_price
bob.buy_widget(5)
print widget_stock_price
________________________________________________________________________

You could make these variables global within the class, but you also need to straighten out your math ...

player_name = raw_input("What is your name?")

class Stock_investor(object):
    wind_stock_price = 50
    empire_stock_price = 50
    widget_stock_price = 50
    def __init__(self, num_wind, num_empire, num_widget, name):
        self.num_wind = num_wind
        self.num_empire = num_empire
        self.num_widget = num_widget
        self.name = name

    def print_stock(self):
        print self.name, "has", self.num_wind, "stocks in wind"
        print self.name, "has", self.num_empire, "stocks in empire"
        print self.name, "has", self.num_widget, "stocks in widget\n"

    def print_name(self):
        print self.name

    def say_hello(self):
        print "Hello, my name is", self.name

    def buy_widget(self, amount):
        self.num_widget += amount
        self.widget_stock_price += amount

    def buy_empire(self, amount):
        self.num_empire += amount

    def buy_wind(self, amount):
        self.num_wind += amount

    def sell_widget(self, amount):
        self.num_widget -= amount

    def sell_wind(self, amount):
        self.num_wind -= amount

    def sell_widget(self, amount):
        self.num_empire -= amount

bob = Stock_investor(5, 5, 10, "Bob")
joe = Stock_investor(5, 5, 7, "Joe")
thor = Stock_investor(5, 5, 5, "Thor")

bob.say_hello()
joe.say_hello()
thor.say_hello()

print widget_stock_price
bob.buy_widget(5)
print widget_stock_price
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.