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

Python: Salted Earth

this is my code, please help I really appreciate it

Ogres are attacking a nearby settlement!

Be careful, though, for the ogres have sown the ground with poison.

Gather coins and defeat the ogres, but avoid the burls and poison!

while True:
enemy = hero.findNearestEnemy()
if enemy.type == “munchkin” or enemy.type == “thrower”:
hero.attack(enemy)
item = hero.findNearestItem()
if item.type ==“coin” or item.type == “gem”:
# Then move and pick it up:
hero.move(item)

Can you please format your code using the button below:
image
(not the quotation marks)

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    # Check the item type to make sure the hero doesn't pick up poison!
    # If the item's type is "gem" or "coin":
    if item.type == "gem" or "coin":
        # Then move and pick it up:
       hero.move(item)

@Saanvi_K I see your problem, hero.move(item) should be hero.move(item.pos) I recommend using hero.moveXY(x, y) because you will only learn about hero.move() in the mountain levels

Wrap this in an if statement.
This:

should be like

yep now the code will work

it still shows this though:

Saanvi, you can’t just use the item as an argument for move. If you want your hero to move to the item, you need to have: hero.move(item.pos)

yeah like this:
hero.moveXY(item.pos.x, item.pos.y)

Hi. Thank you for trying to help, but the person who needs help is no longer active so he/she won’t see your post.
Lydia

2 Likes

Hi I just wanted to ask for help on the code that involves the character to move to the item. Here is my code.

`# Ogres are attacking a nearby settlement!
# Be careful, though, for the ogres have sown the ground with poison.
# Gather coins and defeat the ogres, but avoid the burls and poison!

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    # Check the item type to make sure the hero doesn't pick up poison!
    # If the item's type is "gem" or "coin":
    if item.type == "coin" or item.type == "gem" :
        
        # Then move and pick it up:
        hero.move(48, 42) 
        `
Is the move code correct?

It’s supposed to be hero.moveXY(x, y), not just hero.move(x, y). :slight_smile:
Besides that, I think the code is fine.

Rachel