Build a Planet Class - Build a Planet Class

Tell us what’s happening:

I have done pretty much everything that’s been asked, but still couldn’t pass the test cases. It’s even giving the output what was asked for, yet it’s failing in 18th step.

Your code so far

class Planet:
    def __init__(self, name, planet_type, star):
        if not isinstance(name,  str) or not isinstance(planet_type, str) or not isinstance(star,str):
            raise TypeError("name, planet type, and star must be strings")
        if len(name) == 0 or len(planet_type) ==0 or len(star) ==0 :
            raise ValueError("name, planet_type, and star must be non-empty strings")

        self.name= name
        self.planet_type = planet_type
        self.star = star
    
    def orbit(self):
        return f"{self.name} is orbiting around {self.star}..."
    
    def __str__(self):
        return f"Planet: {self.name} | Type: {self.planet_type} | Star: {self.star}"
    


planet_1 = Planet("Jupiter", "Gas giant", "Sun")
planet_2 = Planet("Earth", "Terrestrial", "Sun")
planet_3 = Planet("Proxima Centauri b", "Exoplanet", "Proxima Centauri")

print(str(planet_1))
print(planet_1.orbit())

print(str(planet_2))
print(planet_2.orbit())

print(str(planet_3))

print(planet_3.orbit())


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:144.0) Gecko/20100101 Firefox/144.0

Challenge Information:

Build a Planet Class - Build a Planet Class

Can you explain what this line does?

As it was asked in the 6th user story, I tried printing the output from str() method from planet object.

This is the str method of your Planet class. It is used by print() on an instance of the class to output what __str__() returns.

str() used like this is a built-in function of Python.

You should print each planet object to see the __str__ method output.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.