我有一个 Python 程序,它生成一个恒星场,并用卫星填充这些恒星。在代码的最后,我有一些嵌套循环来打印星星和它们的卫星,这样我就可以调试生成器了。结果有点让人摸不着头脑。我几乎可以肯定我的星星和卫星是正确生成的,问题出在循环中。编辑:看起来我错了。哎呀。
这是代码:
for s in world.stars:
print s.name
for satellite in s.satellites:
print satellite.name
world.stars
是 Star 对象的列表,其中包含卫星对象的列表。对于这个测试,我生成了三颗星,每颗星有两颗卫星。
这是输出:
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是星名,D6-0是卫星名:D6星的一颗卫星。它没有列出恒星的名称,然后列出它的行星,而是列出了所有恒星名称下的所有行星。看来我不明白for
循环的用法,以及每次迭代是如何初始化的。有人可以向我解释我错在哪里,或者将我链接到可以的资源吗?
奖励:一般来说,有没有比for
循环更好的方法?
编辑:完整代码!巨大的...
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