Simple question - stock market simulation
I'm trying to create an incredibly simple stock market simulator to be used in a childrens classroom and I'm wondering whether someone can point me in the right direction. I basically want to be able to have a stock struct as follows: StockName = "Test" StockPriceList = (12,13,12,10,10,8,10) StockRisk = 0.15 StockQty = 2000 And then have a StockMarket struct that can contain 1 to many Stocks. I then want to be able to iterate through the stocks to perform operations as such: for Stock in StockMarket: I'm not asking for tips on the program itself, I just can't figure out how to get the data structures in place. Would I use Classes or would I use dictionaries? Am I completely off the mark with this? Apologies for such a rudimentary question...I'm very new to all this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple question - stock market simulation
On Feb 9, 3:58 pm, Paul Rubin <http://phr...@nospam.invalid> wrote: > "cptn.spoon" writes: > > I'm not asking for tips on the program itself, I just can't figure out > > how to get the data structures in place. Would I use Classes or would > > I use dictionaries? Am I completely off the mark with this? > > Typically you would use a class for the multi-fielded data structure; > a dictionary is intended more for lookup tables where you don't know > ahead of time what the keys will be. Also, you'd typically use a list > [1,2,3] rather than a tuple (1,2,3) to hold the price list. Thanks Paul! I thought this might be the case. So how would I get the StockMarket class instance to contain many Stock class instances and then be able to iterate through them? I'm guessing the basic structure would be as follows...but I think I'm wrong: class StockMarket: pass class Stock: def __init__(self, stockname, stockpricelist[], stockrisk, stockqty): self.stockname = stockname self.stockpricelist[] = stockpricelist[] self.stockrisk = stockrisk self.stockqty = stockqty Can you possibly point me in the right direction? Thanks Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple question - stock market simulation
On Feb 10, 3:29 am, Anthra Norell wrote: > cptn.spoon wrote: > > I'm trying to create an incredibly simple stock market simulator to be > > used in a childrens classroom and I'm wondering whether someone can > > point me in the right direction. > > > I basically want to be able to have a stock struct as follows: > > > StockName = "Test" > > StockPriceList = (12,13,12,10,10,8,10) > > StockRisk = 0.15 > > StockQty = 2000 > > > And then have a StockMarket struct that can contain 1 to many Stocks. > > I then want to be able to iterate through the stocks to perform > > operations as such: > > > for Stock in StockMarket: > > > I'm not asking for tips on the program itself, I just can't figure out > > how to get the data structures in place. Would I use Classes or would > > I use dictionaries? Am I completely off the mark with this? > > > Apologies for such a rudimentary question...I'm very new to all this. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > If the subject is investment performance, surely your best bet is to > hand out piggy banks. > > Frederic Thank you all for your advice. It's been most helpful! Frederic: Haha, it's more to encourage behaviour and risk management but I agree with your sentiment! -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple question - stock market simulation
On Feb 9, 6:48 pm, "Hendrik van Rooyen" wrote: > "cptn.spoon" wrote: > > On Feb 9, 3:58 pm, Paul Rubin <http://phr...@nospam.invalid> wrote: > > >Thanks Paul! I thought this might be the case. So how would I get the > >StockMarket class instance to contain many Stock class instances and > >then be able to iterate through them? I'm guessing the basic structure > >would be as follows...but I think I'm wrong: > > >class StockMarket: > > pass > > No. > At this level, just use a list of instances of your Stock class. > > - Hendrik How do I get a list of instances of a particular class? Is there a way to do this dynamically? Also, what would be the way of dynamically creating an instance of a class based on user input (ie a user wants to create a new instance of the Stock class via shell input)? I'm not sure if I have the wrong mindset here. Thanks for any help ~Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple question - stock market simulation
On Feb 10, 10:26 am, Robert Kern wrote: > On 2009-02-09 17:10, cptn.spoon wrote: > > > > > On Feb 9, 6:48 pm, "Hendrik van Rooyen" wrote: > >> "cptn.spoon" wrote: > > >> On Feb 9, 3:58 pm, Paul Rubin<http://phr...@nospam.invalid> wrote: > > >>> Thanks Paul! I thought this might be the case. So how would I get the > >>> StockMarket class instance to contain many Stock class instances and > >>> then be able to iterate through them? I'm guessing the basic structure > >>> would be as follows...but I think I'm wrong: > >>> class StockMarket: > >>> pass > >> No. > >> At this level, just use a list of instances of your Stock class. > > >> - Hendrik > > > How do I get a list of instances of a particular class? Is there a way > > to do this dynamically? > > You *can*, but I highly recommend that you don't. Instead, just keep your own > list of instances. When you make a new instance, just append it to the list > (see > below). > > all_stocks = [] > > > Also, what would be the way of dynamically creating an instance of a > > class based on user input (ie a user wants to create a new instance of > > the Stock class via shell input)? > > Define an __init__ method on your class to initialize it from given values. > Use > raw_input() to query the user for information. Convert the text input to > whatever objects your class needs (e.g. if the user entered "10" on the > prompt, > x=raw_input() will return the string '10', so you would do int(x) to get the > integer 10). Now, instantiate your class with the arguments: > > the_stock = Stock(name, risk, initial_price) > > And append it to your list of stocks. > > all_stocks.append(the_stock) > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." > -- Umberto Eco That's exactly what I was after. Thanks Robert! -- http://mail.python.org/mailman/listinfo/python-list