Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,606 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 905 people online right now. Registration is fast and FREE... Join Now!




Python class problems

 
Reply to this topicStart new topic

Python class problems

yanom
post 10 Jul, 2008 - 07:02 AM
Post #1


New D.I.C Head

*
Joined: 5 Apr, 2008
Posts: 46


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Stutzbach
post 10 Jul, 2008 - 07:06 AM
Post #2


New D.I.C Head

*
Joined: 6 Jul, 2008
Posts: 10



Thanked 3 times
My Contributions


The symbol "buy" is part of the stock module, so you need to type "pytrade.buy" rather than just "buy".

Hope that helps!
User is offlineProfile CardPM

Go to the top of the page

linuxunil
post 10 Jul, 2008 - 07:12 AM
Post #3


New D.I.C Head

Group Icon
Joined: 7 Mar, 2006
Posts: 46



Thanked 2 times

Dream Kudos: 125
My Contributions


When using import <module> you still have to declare your name space.

So you would have to use pytrade.buy(stock.green, 2) you also need to do this in your pytrade file for your reality check and in a few other areas.
User is offlineProfile CardPM

Go to the top of the page

ibbie
post 10 Jul, 2008 - 07:31 AM
Post #4


New D.I.C Head

Group Icon
Joined: 7 Jul, 2008
Posts: 8



Dream Kudos: 75
My Contributions


If you really want to access those functions without prefixing the module, you can do something like this:

CODE
from pytrade import buy, sell


That explicitly imports the named functions into your current namespace.
User is offlineProfile CardPM

Go to the top of the page

linuxunil
post 10 Jul, 2008 - 08:18 AM
Post #5


New D.I.C Head

Group Icon
Joined: 7 Mar, 2006
Posts: 46



Thanked 2 times

Dream Kudos: 125
My Contributions


QUOTE(ibbie @ 10 Jul, 2008 - 08:31 AM) *

If you really want to access those functions without prefixing the module, you can do something like this:

CODE
from pytrade import buy, sell


That explicitly imports the named functions into your current namespace.


This is ok if you just have one or two imports. If you have a lot of imports it makes your code less readable and that is one of the strong points of python. It is also better to use import <module> if you have modules that may have conflicting method names. If pytrade.py and stock.py both have buy methods using the from <module> import buy, sell would cause conflicts.

For more information on the differences and when to use each check out Dive Into Python
User is offlineProfile CardPM

Go to the top of the page

yanom
post 11 Jul, 2008 - 06:47 AM
Post #6


New D.I.C Head

*
Joined: 5 Apr, 2008
Posts: 46


My Contributions


pytrade.sell(stock.green, 2) didn't help:

CODE

>>> import pytrade, stock
Welcome to Pytrade, the interactive stockmarket simulator
written in Python, the dynamic, object-oriented scripting
language
>>> pytrade.sell(stock.green, 2)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    pytrade.sell(stock.green, 2)
  File "/home/yanom/bin/pytrade/pytrade.py", line 17, in sell
    if input_stock in stocklist: #another reality check :)
NameError: global name 'stocklist' is not defined
>>>


This post has been edited by yanom: 11 Jul, 2008 - 06:49 AM
User is offlineProfile CardPM

Go to the top of the page

linuxunil
post 11 Jul, 2008 - 08:45 AM
Post #7


New D.I.C Head

Group Icon
Joined: 7 Mar, 2006
Posts: 46



Thanked 2 times

Dream Kudos: 125
My Contributions


Check your other files to make sure that you don't have the same problem in them. I noticed you import stock into pytrade. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:32AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month