I am trying to learn Python. I have created a simple guessing game and i am now starting on a more ambitious project: a stock market game. The program has 2 files: stock.py and pytrade.py. pytrade.py is the engine, and stock.py is designed to be edited by the user to change the stocks and prices used for the game.
stock.py
CODE
import random
#class
class stock:
price = (0, 0) #price of stock
name = "class" #name of stock
currentprice = 0
owned = 0
#/class
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::
green = stock() #instance
green.price = (1, 21) #define
green.name = "green" #define
red = stock() #instance
red.price = (20, 44) #define
red.name = "red" #define
blue = stock() #instance
blue.price = (8, 19) #define
blue.name = "blue" #define
yellow = stock() #instance
yellow.price = (90, 180) #define
yellow.name = "yellow" #define
def market(): #prints a list of stocks & their prices
print green.name
green.currentprice = random.randrange(green.price)
print green.currentprice
print red.name
red.currentprice = random.randrange(red.price)
print red.currentprice
print blue.name
blue.currentprice = random.randrange(blue.price)
print blue.currentprice
print yellow.name
yellow.currentprice = random.randrange(yellow.price)
print yellow.currentprice
stocklist = [green.name, red.name, blue.name, yellow.name]
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pytrade.py
CODE
import stock
print "Welcome to Pytrade, the interactive stockmarket simulator"
print "written in Python, the dynamic, object-oriented scripting"
print "language"
#save = open("save.txt", "r+")
#saving using pickle comming soon
def buy(input_stock, shares):
if input_stock in stocklist: #basic reality check :)
#todo: implement money
input_stock.owned = input_stock.owned + shares
def sell(input_stock, shares):
if input_stock in stocklist: #another reality check :)
#todo: implement money
input_stock.owned = input_stock.owned - shares
however, when i test it in the Python command line:
CODE
>>> import stock
>>> import pytrade
>>> buy(green, 2)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
buy(green, 2)
NameError: name 'green' is not defined
>>>
why does my code not work?
This post has been edited by yanom: 10 Jul, 2008 - 07:04 AM