<div class="gmail_quote">On Tue, Nov 29, 2011 at 2:31 PM, ADRIAN KELLY <span dir="ltr"><<a href="mailto:kellyadrian@hotmail.com">kellyadrian@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div dir="ltr">
i am trying to create a program that will allow users to enter items and their prices; should i be looking at a list, tuple or what?</div></div></blockquote><div><br></div><div>The entering part isn't as important as how you want to display the data. For instance, here's a program that allows the user to input an unlimited amount of data (using Python 3.x):</div>
<div><span style="background-color: transparent; "><br></span></div><div><span style="background-color: transparent; ">while input("Enter q to quit, or an item: ").lower() not in ('q', 'quit', 'goodbye'):</span></div>
<div><span style="background-color: transparent; "> input("Enter the price: ")</span></div><div><span style="background-color: transparent; "><br></span></div><div><span style="background-color: transparent; ">Of course it doesn't store the data, so it's pretty useless. But it does allow the user to input whatever they want.</span></div>
<div><span style="background-color: transparent; "><br></span></div><div><span style="background-color: transparent; ">If you wanted to simply create a collection of items you could do it as a list with alternating values:</span></div>
<div><span style="background-color: transparent; "><br></span></div><div><span style="background-color: transparent; "> inventory = ['Crunchy Frog', 4.13, 'Anthrax Ripple', 12.99999999999, 'Spring Surprise', 0.00]</span></div>
<div><span style="background-color: transparent; "> for x in range(0, len(inventory)-1, 2):</span></div><div> print(inventory[x], inventory[x+1])</div><div><br></div><div>Or as a list of tuples:</div><div> </div>
<div> inventory = [('Norwegian Blue', 500.00), ('Slug', 500.00), ('Cage', 50.00)]</div><div> for item in inventory:</div><div> print(item[0], item[1])</div><div><br></div><div>Or a dictionary:</div>
<div><br></div><div> inventory = {'Spam':5.00, 'Spam on eggs':10.00, 'Spam on Spam':7.50}</div><div> for item, price in inventory.items():</div><div> print(item, price)</div><div><br>
</div>
<div>Or if you wanted to get ridiculous, you could go with a list of classes:</div><div><br></div><div>class Item:</div><div> def __init__(self, desc='', price=0.00):</div><div> self.desc = desc</div><div>
self.price = price</div><div> def __repr__(self):</div><div> return str(self)</div><div> def __str__(self):</div><div> return "{0} - {1}".format(self.desc, self.price)</div><div><br>
</div>
<div>inventory = [Item('Lumberjack', 5.5), Item('Tree', 50), Item('Flapjack', 0.5)]</div><div>for item in inventory:</div><div> print(item)</div><div><br></div><div><br></div><div>It just depends on how complex you want to get!</div>
<div>HTH,</div><div>Wayne</div></div>