I am not sure what to forum to post it in, so I will post this here. I haven’t put this code in panda but I will soon after I figure out this list problem.
I am fairly new to python and so if you can give me any advice on my coding would be great!
So I am currently working on a solar system generator and have a problem with lists/dicts. This is the current code I am working on.
import random
r = {"t": [] }
r["t"] = ['Planet 1', 'Planet 2', 'Planet 3']
y = {'Planet Type': '', 'Planet Name': '', 'Location List': []}
for x in range(3):
planet = random.randint(1,2)
if planet == 1:
y["Planet Type"] = "Terrestrial"
r["t"][x] = y
else:
y["Planet Type"] = "Gas Giant"
r["t"][x] = y
I wanted to roll randomly to see if the planet is a terrestrial planet or a gas giant and then append the type of planet in Y and then update the y to the R dictionary.
When I run my current code it runs the “for - loop” on it runs the r[“t”][x] 3 times and updates all the planets to be what ever it rolled. It then runs the random again and updates all three planets to be the same planet on what ever it rolled again.
I want to update the planet type once for first planet in loop 1 then start loop 2 and update the planet type for that planet then start loop 3 and do the same thing
Can anyone teach me a way to do this?
Also my second question is, In my current program I am using a lot of dictionaries and list to keep every thing neat so it currently looks like this:
planetInfo = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo = {"Star Type": '',"PlanetNum": '',"Planet List":[planetInfo ]}
galaxyList = [systemInfo]
picture of how the code is suppose to look. To view it copy and paste it directly into ur IDE. looks like crap while its on the webpage
galaxyList = [ Star system 1] = {"Star Type": '',"PlanetNum": '',"Planet List":[ planet List]}
[ Star system 2] = {"Star Type": '',"PlanetNum": '',"Planet List":[ planet List]}
[ Star system 3] = {"Star Type": '',"PlanetNum": '',"Planet List":[ planet List]}
[ Star system 4] = {"Star Type": '',"PlanetNum": '',"Planet List":[ Planet 1 ] = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [ loaction on the planet List],}}
[ Planet 2 ] = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [ loaction on the planet List],}
[ Planet 3 ] = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [ loaction on the planet List],}
[ Planet 4 ] = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [ loaction on the planet List],}
How it is suppose to work, the GalaxyList holds a list containing all the systems (or systemInfo).
The sytemInfo is a dictionary that saves specific characteristic of that system as a keys.
The systemInfo has a Planet List that holds all the planets for that system in it. Each planet is also its own dictionary that holds the planets specific characteristics as keys.
I know I can save all that information in lists, but it guess messy fast, and if you don’t remember what position a value is in the list, your screwed. That is why I wanted to use dictionaries to make sorting the many different data types easier.
Is the way I am storing my data a good way? Or is there a better/faster way to do it?
Is there a different in performance between lists and dicts?
[/code]