Help double cheek python SOLVED


#1

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)


#2

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.


#3

sry chaboi im new idk how


#4

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)

#5

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


#6

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


#7

“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.


#8

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