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())
1 Like

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.

1 Like

still not working:

1 Like

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

1 Like

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())
1 Like

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

1 Like

but thanks for the help!

1 Like

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.

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.