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

[SOLVED] Help double cheek python

can someone pls help me
here is my code

First, defeat 6 ogres.

Then collect coins until you have 30 gold.

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(49, 36)

This loop is executed while you have less than 30 gold.

while hero.gold < 30:
# Find and collect coins.
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)

Please learn to post your code properly. It’s really easy and only requires a very small amount of effort.

To post your code from the game, use the </> button or it won’t format properly. When you click the </> button, the following will appear:

Please paste ALL of your code inside the triple back tick marks.

``` <— Triple back tick marks.

Paste ALL of your code in here.

``` <— Triple back tick marks.

There are many people here willing and able to help. If you use the </> button correctly, then ALL of your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If ALL of your code isn’t formatted like the code above, then you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well.

Thank you.

sry chaboi im new idk how

1 Like

k so this is my code

 First, defeat 6 ogres.
# Then collect coins until you have 30 gold.

# 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(49, 36)

# This loop is executed while you have less than 30 gold.
while hero.gold < 30:
    # Find and collect coins.
    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)

the targetpos= coin.pos
it said coin is not defined

The error message is correct; you have not defined coin. Also conspicuously absent is an if conditional looking for the existence of coins.

1 Like

“Also conspicuously absent is” a check for enemy health. The counter holds the value of attackedOgres not defeatedOgres If your hero has a simple sword or the enemies are a bit more powerful this will produce an infinite loop.

1 Like

oh k thanks i already finshed it but thanks for the help. :grinning:

I don’t understand why I get that error. I reloaded it 20 times; it still gives me that error. How is this fixable? Also I’m using a wizard with a vine staff.

I suppose because the default suggested code is wrong. See Eliminating the loop in if (enemy) while(enemy.health > 0) hero.attack(enemy); for hints.
Edit: Checked with wizard and wine staff and got infinite loop if not checking the health in while loop. See my previous post and check the topic Eliminating the loop…

Ok, I got pass that, but coins aren’t spawning. It was a level design error, or it was a bug.