I have a Python program that generates a field of stars, and populates those stars with satellites. At the end of the code, I have some nested loops to print the stars and their satellites so I can debug the generator. The result is a bit bewildering. I am almost certain that my stars and satellites are generated correctly, and that the issue is in the loops. EDIT: Looks like I was wrong. Whoops.
This is the code:
for s in world.stars:
print s.name
for satellite in s.satellites:
print satellite.name
world.stars is a list of Star objects, which contain lists of satellite objects. For this test, I generated three stars with two satellites each.
This is the output:
D6
D6-0
D6-1
S11-0
S11-1
M19-0
M19-1
S11
D6-0
D6-1
S11-0
S11-1
M19-0
M19-1
M19
D6-0
D6-1
S11-0
S11-1
M19-0
M19-1
D6 is a star name, and D6-0 is a satellite name: a satellite of the D6 star. Instead of listing a star's name then its planets, it lists all planets beneath all star names. It seems that I don't understand the usage of for loops, and how each iteration is initialized. Could someone explain to me where I am wrong, or link me to a resource that could?
BONUS: In general, is there a better way than for loops?
EDIT: Full code! Huge...
class World():
stars = []
max_stars = 3
used_sat_names = []
def __init__(self):
print 'World Created'
def genUniverse(self):
last_distance_from_earth = 0
for x in xrange(0, self.max_stars):
star = Star(last_distance_from_earth)
satellite_max = random.randint(0,5)
for s in xrange(0, 2):
last_distance_from_star = 0
satellite = Satellite(star.name, satellite_max, s)
star.satellites.append(satellite)
last_distance_from_earth = star.distance_from_earth
self.stars.append(star)
class Star(object):
name = ''
distance_from_earth = 0
kind = ''
satellites = []
def __init__(self, last_distance):
self.distance_from_earth = last_distance + random.randint(4,8)
star_kind_list = (
('Neutron', 3, 'N'),
('Dwarf', 10, 'D'),
('Small', 22, 'S'),
('Medium', 30, 'M'),
('Large', 20, 'L'),
('Giant', 10, 'G'),
('Supergiant', 5, 'S')
)
kind_index = WeightedChoice(star_kind_list).nextIndex()
self.kind = star_kind_list[kind_index][0]
self.name = star_kind_list[kind_index][2] + '%i'%(self.distance_from_earth)
print 'Star called %s created!'%(self.name)
class Satellite(object):
name = ''
star_name = ''
distance_from_star = 0
size = ''
kind = ''
moons = []
def __init__(self, star_name, satellite_max, satellite_number):
self.star_name = star_name
self.name = '%s-%s'%(star_name, satellite_number)
satellite_size_list = (
('Dwarf', 10),
('Small', 30),
('Medium', 20),
('Large', 20),
('Giant', 20),
)
self.size = WeightedChoice(satellite_size_list).next()
print '%s has a satellite called %s'%(self.star_name, self.name)
world = World()
world.genUniverse()
for s in world.stars:
print s.name
for satellite in s.satellites:
print satellite.name
satellites, instead of instance variables. Can you show us the code for your class?world.starsare instances of (whatever you have named it).