[SOLVED] Ice hunter SOS HELP

Here is my code:

# Hunt for 4 yaks. Choose only the small ones.
# Small yak names contain a "bos" substring.

# This function checks if a word contains a substring.
def isSubstring(word, substring):
    # We iterate through the start indexes only.
    rightEdge = len(word) - len(substring)
    # Loop through the indexes of the word.
    for i in range(rightEdge + 1):
        # For each of them loop through the substring
        for j in range(len(substring)):
            # Use an offset for the word's indexes.
            shiftedIndex = i + j
            # If letters aren't the same:
            if word[shiftedIndex] != substring[j]:
                # Check the next start index in the word.
                break
            # If it was the last letter in the substring:
            if j == len(substring) - 1:
                # Then the substring is in the word.
                return True
    # We haven't found the substring in the word.
    return False

# Loop through all enemies.
enemies = hero.findEnemies()
for e  in range(len(enemies)):
    enemy = enemies[e]
    # Use the function isSubstring to check
    # if an enemy.id contains "bos":
    if enemy.id 
        # Then defeat it.
        

Here check if what isSubstring(enemy.id, “bos”) returns is True and if it is, while enemy.health > 0, attack the enemy.

Andrei

1 Like

I dont get it here is my code:

# Hunt for 4 yaks. Choose only the small ones.
# Small yak names contain a "bos" substring.

# This function checks if a word contains a substring.
def isSubstring(word, substring):
    # We iterate through the start indexes only.
    rightEdge = len(word) - len(substring)
    # Loop through the indexes of the word.
    for i in range(rightEdge + 1):
        # For each of them loop through the substring
        for j in range(len(substring)):
            # Use an offset for the word's indexes.
            shiftedIndex = i + j
            # If letters aren't the same:
            if word[shiftedIndex] != substring[j]:
                # Check the next start index in the word.
                break
            # If it was the last letter in the substring:
            if j == len(substring) - 1:
                # Then the substring is in the word.
                return True
    # We haven't found the substring in the word.
    return False

# Loop through all enemies.
enemies = hero.findEnemies()
for e  in range(len(enemies)):
    enemy = enemies[e]
    # Use the function isSubstring to check
    # if an enemy.id contains "bos":
    if enemy.id, "bos":
        # Then defeat it.
        
1 Like

Here try to put

if isSubstring(enemy.id, "bos"):
    while enemy.health > 0:
         #here attack the enemy

Andrei

1 Like

Doesn’t work :disappointed:

Can I have a look at the code?

Andrei

1 Like
# Hunt for 4 yaks. Choose only the small ones.
# Small yak names contain a "bos" substring.

# This function checks if a word contains a substring.
def isSubstring(word, substring):
    # We iterate through the start indexes only.
    rightEdge = len(word) - len(substring)
    # Loop through the indexes of the word.
    for i in range(rightEdge + 1):
        # For each of them loop through the substring
        for j in range(len(substring)):
            # Use an offset for the word's indexes.
            shiftedIndex = i + j
            # If letters aren't the same:
            if word[shiftedIndex] != substring[j]:
                # Check the next start index in the word.
                break
            # If it was the last letter in the substring:
            if j == len(substring) - 1:
                # Then the substring is in the word.
                return True
    # We haven't found the substring in the word.
    return False

# Loop through all enemies.
enemies = hero.findEnemies()
for e  in range(len(enemies)):
    enemy = enemies[e]
    # Use the function isSubstring to check
    # if an enemy.id contains "bos":
    if isSubstring(enemy.id, "bos"):
        while enemy.health > 0:
            # Then defeat it.
        

After this line attack the enemy.

Andrei

1 Like

Bye :smiley: :smile: :grinning: :smiley: :slightly_smiling_face:

1 Like

Congratulations for completing the level! :partying_face: :grin:

Andrei

1 Like