-6

All the codes are given except for the def merge() in class Star_cluster

class Planet:
    def __init__(self, a_str, a_boolean, an_int):
        self.type = a_str
        self.in_habitable_zone = a_boolean
        self.number_of_moons = an_int


class Star:

    def __init__(self, a_str, a_float, a_planet_list):
        self.name = a_str
        self.surface_temperature = a_float  # in Kelvin
        self.orbiting_planets = a_planet_list  # list of Planet-objects


class Star_cluster():

    def __init__(self):
        self.star_list = []  # list of Star-objects
        self.total_number_of_planets = 0

    def add(self, star):
        self.star_list.append(star)
        self.total_number_of_planets += len(star.orbiting_planets)

    def merge(self, a_star_cluster):
        self.star_list.append(a_star_cluster)


planet1 = Planet('rocky', True, 1)

planet2 = Planet('gas giant', False, 9)
planet3 = Planet('rocky', True, 3)

star1 = Star('Alpha Centauri', 9001.0, [])
star2 = Star('Betelgeuze', 3000.0, [planet1])
star3 = Star('Tau Ceti', 7000.0, [planet2, planet3])

sc1 = Star_cluster()
sc1.add(star1)

sc2 = Star_cluster()
sc2.add(star2)
sc2.add(star3)

sc1.merge(sc2)

print "should be 3: %d" % (len(sc1.star_list))

The only thing we need to put inside this code is the def merge but I keep getting 2 instead of 3. We have an exam tomorrow but our teacher does not give us any answers....

  • 3
    This is too much code to have us debug for you, especially when you haven't even described the code. Have you done any debugging? – Carcigenicate Dec 17 '17 at 18:09
  • I'm going to take a stab though that you shouldn't be using `append` in `merge`. You seem like you need to concatenate the lists, not append one to the other. – Carcigenicate Dec 17 '17 at 18:10
  • No we did not because they do not expect it on the exam. The only thing we need to do is to fill in the def merge(self, a_star_cluster). What we need is to merge sc1 with sc2. – Kaan Durmus Dec 17 '17 at 18:11
  • I'm not sure what that comment was responding to. If you're saying you didn't debug because you don't have to for the exam, that's the wrong way to think about it. You need to debug code regardless. It's the means to the end. – Carcigenicate Dec 17 '17 at 18:12
  • Thanks for your answer. The thing is we do not to debug because the code is already given correct (I hope). So we need just to put in some sort of small code to def merge – Kaan Durmus Dec 17 '17 at 18:15
  • Possible duplicate of [How to concatenate two lists in Python?](https://stackoverflow.com/questions/1720421/how-to-concatenate-two-lists-in-python) – Carcigenicate Dec 17 '17 at 18:15
  • Read the link I posted, and use its suggestion instead of `append` in `merge`. `append` doesn't merge lists. – Carcigenicate Dec 17 '17 at 18:16
  • And you need to merge the star_list in the Star_Clusters, not the star clusters themselves. – Carcigenicate Dec 17 '17 at 18:18
  • @KaanDurmus: Obviously you should have debugged the code. You could just have printed `sc1.star_list` and you would have seen the problem. – Matthias Dec 17 '17 at 19:13
  • What will be the code for def merge? – Kaan Durmus Dec 17 '17 at 19:35

1 Answers1

0

You are on the right track, however, try printing the sc1.star_list itself. You should see:

[<__main__.Star instance at 0x10281c680>, <__main__.Star_cluster instance at 0x10281c7a0>]

Notice that the second item is a star cluster. This is because in your Star_Cluster.merge(), you are appending the other star cluster itself, and not its stars. Instead, use:

def merge(self, a_star_cluster): self.star_list.append(a_star_cluster.star_list) self.total_number_of_planets += a_star_cluster.total_number_of_planets

This will copy the stars and remember how many planets the cluster now has, but without copying the other star cluster itself.

Tkasriel
  • 3
  • 5