Build a Planet Class - Build a Planet Class

Tell us what’s happening:

I cant seem to do challenge 18 that is the only one that has an x. How do I fix this so it has a check mark because the output of my code is the desired output yet I still receive the message the challenge 18 is wrong. Thanks

Your code so far

class Planet:
    def __init__(self, name, planet_type, star):
        
        self.name = name
        self.planet_type = planet_type
        self.star = star
        
        for arg_name, value in [('name', name), ('planet_type', planet_type), ('star', star)]:
            if not isinstance(value, str):
                raise TypeError('name, planet type, and star must be strings')

            if value == '':
                raise ValueError('name, planet_type, and star must be non-empty strings')

    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('Earth', 'Terrestial', 'Sun')
planet_2 = Planet('Mars', 'Terrestial', 'Sun')
planet_3 = Planet('Venus', 'Terrestial', 'Sun')

print(planet_1.orbit())
print(planet_2.orbit())
print(planet_3.orbit())

print(planet_1.__str__())
print(planet_2.__str__())
print(planet_3.__str__())


                


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Planet Class - Build a Planet Class

What’s the meaning of a method that has double underscores?

What is the __str__ method used for?

wait I just realized it doesnt need to be directly stated in the print since its automatic as a built in method thanks so much!

1 Like

its used to call objects output/results as string after being processed by code above or by other functions.

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