Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Computer science 5: level 18 twins power (python)

in my code, the line i highlighted cannot run and it says TyperError: need an object, im not sure what i need to do to fix my code.

here is my code.

# There are four pairs of twins, they should pray by pairs.
# You need to find twins and call them.

# Twins have the same names, only the last letter is different.
# This function checks if the pair of units are twins.
def areTwins(unit1, unit2):
    name1 = unit1.id
    name2 = unit2.id
    if name1.length != name2.length:
        return False
    for i in range(name1.length - 1):
        if name1[i] != name2[i]:
            return False
    return True

# Iterate over all pairs of paladins and
#  say() their name by pairs if they are twins.
twins = hero.findFriends()    #   <--------------------- here is my problem/error
for i in len(twins) :
    for j in len(twins) :
        if i != j and areFriends(twins[i], twins[j]) :
            hero.say(twins[i].id + " " + twins[j].id)
# For example: hero.say("NameTwin1 NameTwin2")
hero.moveXY(64, 36)
hero.moveXY(14, 36)
# When twins are in their spots, lure the ogre.
# Don't be afraid of beams - they are dangerous only for ogres.

Instead of this, put:
areTwins(twins[i], twins[j])

I changed it, but the line I highlighted in my code (twins = hero.findFriends()) keeps saying that I need an object there. It still has the same error for some reason.

I figured out the error!

Here put:

for i in range(len(twins))

And here put:

for j in range(len(twins))

Now have you completed the level?

2 Likes

Yes, thank you for your help! I really appreciated it!!

1 Like

No problem! And congratulations on completing the level! :partying_face:

2 Likes