[SOLVED] Help With Double Cheek

# This variable is used for counting ogres.
defeatedOgres = 0

# This loop is executed while defeatedOgres is less than 6.
while defeatedOgres < 6:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        defeatedOgres += 1
    else:
        hero.say("Ogres!")

# Move to the right side of the map.
hero.moveXY(65, 53)

# This loop is executed while you have less than 30 gold.
while hero.gold < 30:
    # Find and collect coins.
    hero.findNearestItem()
    if item and item.type == "coin":
        hero.findNearestItem()
        targetpos = coin.pos
        hero.move(target.pos)
    # Remove this say() message.
    hero.say("I should gather coins!")

# Move to the exit.
hero.moveXY(76, 32)

it is saying that “item is not defined” when I have a hero.findNearestItem() statement.
please help!

The problem is here:

    hero.findNearestItem() # <=== this statement does nothing...you need to define a variable
    if item and item.type == "coin":
        hero.findNearestItem() # <=== why do this again?
        targetpos = coin.pos # <=== coin is not defined
        hero.move(target.pos) # <=== target is not defined
    # Remove this say() message.
    hero.say("I should gather coins!")

how do I define the variable?

You name the variable and define what it equals…like you did here:

targetpos = coin.pos

‘targetpos’ is your vaiable name
‘= coin.pos’ is the definition (except that coin has not been defined)

I defined the item and now it is saying that “coin” is not defined, and I do not know how to define it. it is this section of code.

if item and item.type == "coin":
        targetpos = coin.pos
        hero.move(target.pos)

define it like this:

Coin = hero.findItems(hero.findnearestItem())

# or do it like dis

Coin = hero.findnearestItem

its not target.pos it is coin.pos

hey I fixed the code to look like this

defeatedOgres = 0
while defeatedOgres < 6:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        defeatedOgres += 1
    else:
        hero.say("Ogres!")
hero.moveXY(53, 37)
while hero.gold < 30:
    item = hero.findNearestItem()
    if item and item.type == "coin":
        coinpos = coin.pos
        Coin = hero.findItems(hero.findnearestItem())
        Coin = hero.findnearestItem
        hero.move(coin.pos)
    hero.say("I should gather coins!")
hero.moveXY(76, 32)
and  the hero is still not moving. Why?

Edit: this pertains to your second code posting, not the one you just posted.

In that if block, you have four different variables.

coin (not defined)
item (defined above?)
targetpos (defined by the non-defined ‘coin’)
target (not defined)

Why not just stick with a single variable? I’m assuming that you defined item as equaling nearestItem, right? If yes, then all you need is:

    if item and item.type == "coin":
        hero.moveXY(item.pos.x, item.pos.y)

it worked thanks so much

2 Likes

if you capitalized it then you should do hero.move(Coin.pos) #capital letters are important

1 Like