Any tips on my code? 2

HI GUYS!
ok, i wrote this code, it’s for exercise 3 in Python: Object-Oriented Programming | Codecademy

class Cat: def __init__(self, input_name, input_breed,is_adopted = False, input_age = 0): self.name = input_name self.breed = input_breed self.age = input_age self.is_cuddly = True self.is_adopted = is_adopted self.family = [] def adopt(self,kitty): # if self.age >= 2 and (kitty.is_adopted) : if(kitty.is_adopted) : kitty.is_adopted = True self.family.append(kitty.name) kitty.family.append(self.name) print("{cat_one} has adopted {cat_two}!welcome to the family {cat_two} !".format(cat_one= self.name,cat_two = kitty.name)) cat_one = Cat("Leo", "Tabby", 3,True) cat_two = Cat("tasha","syberian",0.2,False) cat_one.adopt(cat_two)

it’s meant to run adopt function and print Leo has adopted tasha!welcome to the family tasha !
but it doesn’t work! i happen to know it’s because of the if statement i wrote. if i change it to if(kitty.is_adopted) : instead, it’ll work just fine, but that makes me wonder, why the if statement don’t work? how do if statements work in objects?

When you creating instances of the Cat class, look at the order in which you are passing the arguments.

cat_one = Cat("Leo", "Tabby", 3, True)
cat_two = Cat("tasha","syberian", 0.2, False)

Compare it with the expected order,

def __init__(self, input_name, input_breed, is_adopted = False, input_age = 0):

Is the order of the arguments correct?

I don’t have access to the exercise, so I don’t know the specifications. But is the condition supposed to be:

if (self.age >= 2) and (kitty.is_adopted):

or is it supposed to be:

if (self.age >= 2) and (not kitty.is_adopted):

As I said, I can’t view the instructions. I don’t know what has been mentioned about age and is_adopted in the instructions. Is the age supposed to be greater or equal to 2 or is it supposed to be less than 2? I don’t know. You will have to read the instructions carefully and translate it into the proper code.

so you mean the code is right and the orders are scrambled so it’s fed some str instead of age int?
also, there wasn’t any instructions really, it just said write a code that one entity is altering another entity’s attributes , if that helps the context

p.s: ok that didn’t work…

class Cat: def __init__(self, input_name, input_breed,input_age,is_adopted): self.name = input_name self.breed = input_breed self.age = input_age self.is_cuddly = True self.is_adopted = is_adopted self.family = [] def adopt(self,kitty): if self.age >= 2 and (kitty.is_adopted) : # if(kitty.is_adopted) : kitty.is_adopted = True self.family.append(kitty.name) kitty.family.append(self.name) print("{cat_one} has adopted {cat_two}!welcome to the family {cat_two} !".format(cat_one= self.name,cat_two = kitty.name)) cat_one = Cat("Leo", "Tabby", 3,True) cat_two = Cat("tasha","syberian",0.2,False) cat_one.adopt(cat_two)

i’ve checked and made sure vlaues are matched with their right key,but still doesn’t print anything

Your condition is:

if self.age >= 2 and (kitty.is_adopted) :

The first operand evaluates to True as cat_one’s age is 3.
The second operand evaluates to False as cat_two’s is_adopted value is False.
Hence, True and False evaluates to False. The condition is not met and nothing is printed.

  • If you don’t want to change the condition, then you should change cat_two’s is_adopted value to True.
# You wrote:
cat_one = Cat("Leo", "Tabby", 3,True)
cat_two = Cat("tasha","syberian",0.2,False)
cat_one.adopt(cat_two)

# Change it to:
cat_one = Cat("Leo", "Tabby", 3,True)
cat_two = Cat("tasha","syberian",0.2,True)
cat_one.adopt(cat_two)
  • Alternatively, you can change the condition to:
if self.age >= 2 and (not kitty.is_adopted) :
...
...
cat_one = Cat("Leo", "Tabby", 3,True)
cat_two = Cat("tasha","syberian",0.2,False)
cat_one.adopt(cat_two)

It depends on what logic you are trying to implement, but I suspect the second option of modifying the condition is probably what you want. If the second cat is not already adopted, then you want to allow it’s adoption.

1 Like

i see what you mean, gosh, mb! it did work…
ty for the help

1 Like