[SOLVED] Do you know whats wrong? The spy among us (python)

# The inner gate can hold for a long time.
# However, one of these peasants is an OGRE SPY!
# There is a hint! The spy's name has the letter "z"

# This function checks for a specific letter in a word.
# A string is just an array! Loop over it like an array
def letterInWord(word, letter):
    for i in range(len(word)):
        character = word[i]
        # If character is equal to letter, return True
        return True
    # The letter isn't in the word, so return False
    return False


spyLetter = "z"
friends = hero.findFriends()

for friend in friends:
    friendName = friend.id
    if letterInWord(friendName, spyLetter):
        # Reveal the spy!
        hero.say("yozrik" + " is a spy!")
    else:
        hero.say("yozrik" + " is a friend.")

1 Like
# The inner gate can hold for a long time.
# However, one of these peasants is an OGRE SPY!
# There is a hint! The spy's name has the letter "z"

# This function checks for a specific letter in a word.
# A string is just an array! Loop over it like an array
def letterInWord(word, letter):
    for i in range(len(word)):
        character = word[i]
        # If character is equal to letter, return True
        return True
    # The letter isn't in the word, so return False
    return False


spyLetter = "z"
friends = hero.findFriends()

for friend in friends:
    friendName = friend.id
    if letterInWord(friendName, spyLetter):
        # Reveal the spy!
        hero.say("YOZRIK" + " is a spy!")
    else:
        hero.say(friendName + " is a friend.")

Please help. (2000000000 chars)

why YOZRIK ? (2000000000 chars)

His name has a Z in it.

With regard to the letterInWord function, you forgot the if conditional needed there. Look at the comments.

# if character is equal to letter, return True

You should do that and make sure that the return True statement is inside that if conditional.

With regard to your for loop, what if you get a different seed and there is no one named Yozrik? Then your code will fail to find the spy. That’s why we use variables instead. You’ve defined and used the variable friendName properly in the else conditional. Why not use it in the if conditional?

1 Like

The first letter of a name is always in capitals, while the other letters in the name is in lowercase

@Seojin_Roy_Lee

It’s a bad idea to use names at all. When submitting, you get a random seed and that seed may not even have a character named Yozrik. That’s why we use variables instead.

Does this stay the same or do you change it?

hero.say(friendName + " is a spy!")
# Reveal the spy!
        hero.say(friendName + " is a spy!")

This one you did correctly. It should stay the way it is.

This is mine now.

# The inner gate can hold for a long time.
# However, one of these peasants is an OGRE SPY!
# There is a hint! The spy's name has the letter "z"

# This function checks for a specific letter in a word.
# A string is just an array! Loop over it like an array
def letterInWord(word, letter):
    for i in range(len(word)):
        character = word[i]
        # If character is equal to letter, return True
        if character == ("Z"):
            return True
            # The letter isn't in the word, so return False
    return False


spyLetter = "z"
friends = hero.findFriends()

for friend in friends:
    friendName = friend.id
    if letterInWord(friendName, spyLetter):
        # Reveal the spy
        hero.say(friendName + " is a spy!")
    else:
        hero.say(friendName + " is a friend.")

I added this.

# If character is equal to letter, return True
        if character == ("Z"):
            return True

And it still doesn’t work, does it? That’s because Z and z and two completely different characters. Look closely at your code.

Really though, in your function you shouldn’t be using the actual letter “z”. Try using the variable “letter” instead of the actual letter (z) itself (“letter” is from the arguments in the function name). That way, if the global variable, spyLetter, changes to a different letter, your function and code still work regardless of what that letter is.

Does that make sense?

1 Like

I passes the level thanks :smiley:

abc(20 character20++++++++++)

Bruh it’s been 5 years can you not please

not what (Monkey 20+++)

not revive topics. DO u understand what NOT means?

1 Like