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


#1
# 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.")


#2
# 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.")


#3

Please help. (2000000000 chars)


#4

why YOZRIK ? (2000000000 chars)


#5

His name has a Z in it.


#6

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?


#7

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


#8

@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.


#9

Does this stay the same or do you change it?

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

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

#11

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


#12

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.")


#13

I added this.

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

#14

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?


#15

I passes the level thanks :smiley: