Build a Planet Class - Build a Planet Class

Tell us what’s happening:

Why doesn’t this work?
It says I have to assign the parameters to self.«parameter» in the init method and I did it. So I don’t understand whats wrong, because it’s giving me the correct output.

Your code so far

class Planet:
    def __init__(self, name, planet_type, star):
        if not (isinstance(name, str) and isinstance(planet_type, str) and isinstance(star, str)):
            raise TypeError('name, planet type, and star must be strings')
        elif not (name and planet_type and star):
            raise ValueError('name, planet_type, and star must be non-empty strings')
        else:
            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('terra', 'pedra', 'sol')
planet_2 = Planet('jupier', 'gasoso', 'sol')
planet_3 = Planet('marte', 'rochoso', 'sol')

print(planet_1)
print(planet_2)
print(planet_3)

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Planet Class - Build a Planet Class

Welcome to the forum @bxhdjzj !

Please double check your if statement.

I just took this out of the if statement and it worked.

I think i understood why it was wrong. I thought it should be in a if statement, but it doesn’t need to because i’m raising errors.

Please correct me if I’m wrong and thank you for your help!

Please show your updated code…and your if statement is not doing what you expect.

class Planet:
   def __init__(self, name, planet_type, star):
       if not (isinstance(name, str) and isinstance(planet_type, str) and isinstance(star, str)):
           raise TypeError('name, planet type, and star must be strings')
       elif not (name and planet_type and star):
           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('terra', 'pedra', 'sol')
planet_2 = Planet('jupier', 'gasoso', 'sol')
planet_3 = Planet('marte', 'rochoso', 'sol')


print(planet_1)
print(planet_2)
print(planet_3)


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

Your code passes for me…and I apologize. I overlooked the parentheses around your isinstance checks in your first if statement. We’ve had multiple users create faulty if statements lately…

1 Like

It’s ok. I’m just happy I made everything correct!

And thank you again for your help