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

Echo of war- string.contains() not working

Hi,

I am trying to do the echo of war level. I’m trying to use the string.contains() method but am getting the following error (I know I could do this by iterating through the characters of the string in a loop but I want to understand why the string.contains() is not working)

This is the error message I am getting

Here is my code.

# Destroy 5 robobombs. Some of them are old and safe.
# Old (safe) bombs have the certain letter in their id.

# This function checks if searchLetter is in searchWord.
def isLetterInWord(searchWord, searchLetter):
    # Complete the function.
    if searchWord.contains(searchLetter):
        return True
    return False

# The engineer knows how the old robots are marked.
engineer = hero.findFriends()[0]
safeLetter = engineer.safeLetter

enemies = hero.findEnemies()
for index in range(len(enemies)):
    enemy = enemies[index]
    if enemy and isLetterInWord(enemy.id, safeLetter):
        # Destroy the enemy if it's safe.
        while enemy.health > 0:
            hero.attack(enemy)


I want to know what I’m doing wrong with contains not an alternative solution to the problem please (which I know how to do)

Maybe try enemy.name or something instead of enemy.id, except if you are sure it is the result you are expecting from enemy.id

can u give this level link
(edit)
nvm alr got it

instead of

if searchWord.contains(searchLetter):

try change it to

`for index in range(len(word)):`

I think he meant by this that he already knows this way, but he wants to know why does this function of string.contains not work.

2 Likes

ah sorry my mistake i didn’t read that :grinning_face_with_smiling_eyes:

1 Like

You can do

if searchLetter in searchWord

instead

not

if searchLetter is in searchWord

?

Nevermind. (20 characters)

Doesn’t work.

Try putting this definition contain function

def contains (word, letters):
    for i in range(len(word)):
        e = None
        for j in range(len(letters)):
            if word[i + j] == letters[j]:
                continue
            e = 0
            break
        if e == None:
            return True
    return False

no this would return a boolean (true or false) it’s not the index if that’s what you were looking for… I don’t think you can use hero.say with a boolean

1 Like

You can

hero.say(False) # or hero.say(false); if you are using JavaScript

I’m the one who made it. xD

1 Like

Yep! (20000000000000)