School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

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




[python] Need help with a simple table

 

[python] Need help with a simple table

xZachtmx

25 Jun, 2009 - 02:33 PM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 38



Thanked: 1 times
My Contributions
Ok i need to make a table to keep track of a list of things in my game's solar system.


so i need so do like
sol = #mytable here

And so there is going to be two columns in the table:

Object Name l Object Type
-------------------------------------
Earth l Planet
Iss l Satellite
Sun l Star

So the list would go on. First of all , how do i create this two dimensional table?

And the next part to my question is:

WHenever the player enters a new solar system. I need it to render the various objects. So for example. I have a planet class. But i need it to detect which objects in that list have the "type" planet

So i want to do like:

foreach planet IN sol:
#Render

But i know that isnt the right syntax. SO how would i do that?


And another thing that would help me is if its possible to make a 3 dimensional table?
Like instead of making one of these 2d tables for each of the areas. I could have one big table. And so if that is possible how do i do that?

Sorry if i was unclear i would be happy to clarify anything and thanks for your help biggrin.gif

User is offlineProfile CardPM
+Quote Post


code_m

RE: [python] Need Help With A Simple Table

25 Jun, 2009 - 07:37 PM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
The quick and easy way: use a dictionary.

a dict type is having one variable "define" another, and there isn't any order to it (in theory), as in you can't use index positions like you could if it were a list.

A basic dictionary:
python
solar_system = {"Sun": "Star", "Earth": "Planet", "ISS": "Satellite", "Moon": "Satellite"}
any type before the ":" is known as the key, and any type after is known as the value.

so say you wanted to see way objects were of the same value:
python
>>> solar_system["Sun"] == solar_system["Moon"]
False
>>> solar_system["ISS"] == solar_system["Moon"]
True
Of course there is clever ways to iterate over keys and values, but a quick glance at pydoc should help out.

There is also a second version of dict called collections.defaultdict ... a defaultdict is the same except instead of raising a KeyError when a key isn't found, it simply creates a new key (this is great for input!)
User is offlineProfile CardPM
+Quote Post

xZachtmx

RE: [python] Need Help With A Simple Table

26 Jun, 2009 - 04:20 AM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 38



Thanked: 1 times
My Contributions
ok one more question. How can i do a foreach loop with this?
User is offlineProfile CardPM
+Quote Post

shadhin

RE: [python] Need Help With A Simple Table

26 Jun, 2009 - 05:35 PM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Sep, 2008
Posts: 421



Thanked: 19 times
Dream Kudos: 3650
My Contributions
You can loop through a dictionary by this way-

CODE

for i,j in solar_system.items():
    print "Object Name:%s, Object Type:%s"%(i,j)

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:03AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month