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

[SOLVED] Cluttered Corridors - event.message error

Hi, when I execute the codes, it gave me error:

and here are my codes:

def onHear(event):
    word = event.message.toLowerCase()
    # Convert the word to lower case.
#    word.toLowerCase()
    if word == "north":
        pet.moveXY(pet.pos.x, pet.pos.y + 16)
    elif word == "east":
        pet.moveXY(pet.pos.x + 12, pet.pos.y)
    elif word == "south":
        pet.moveXY(pet.pos.x, pet.pos.y - 16)
    elif word == "west":
        pet.moveXY(pet.pos.x - 12, pet.pos.y)

# Assign the event handler for the pet's "hear" event.
pet.on("hear", onHear())

You do need to separate the definition of word and the conversion toLowerCase in to separate statements.

First, define the variable ‘word’. Then, convert ‘word’ by redefining it, using the .toLowerCase parameter.

still not working:

Repost your code please…it looks good, but is obviously having probs.

sure thing:

def onHear(event):
    word = event.message
    # Convert the word to lower case.
    word = word.toLowerCase()
    if word == "north":
        pet.moveXY(pet.pos.x, pet.pos.y + 16)
    elif word == "east":
        pet.moveXY(pet.pos.x + 12, pet.pos.y)
    elif word == "south":
        pet.moveXY(pet.pos.x, pet.pos.y - 16)
    elif word == "west":
        pet.moveXY(pet.pos.x - 12, pet.pos.y)

# Assign the event handler for the pet's "hear" event.
pet.on("hear", onHear())

hey mate, I just found the issue, I shouldn’t put"()" in the event handler ‘onHear’ at the bottom, the codes work now

but thanks for the help!

heh…it’s always the silly things. I was a bit confused by this at first myself…still not sure I fully understand the logic.

However, in your final statement, you are calling onHear as if it were a function. If it were a function, then the trailing () would be required. But, as an event handler, it steps outside of the rules of a function…delete the parens ‘()’ and give it another go.

1 Like