A fine mint Python


#1

Hi, I talk Spanish but all the people here talk in English, so I’ll talk you my problem in English. I have a trouble with this level, my code is:
def pickUpCoin():
coin = hero.findNearestItem()
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)

Write the attackEnemy function below.

Find the nearest enemy and attack them if they exist!

def findandattackenemy():
hero.findNearestEnemy()
if enemy:
hero.attackEnemy
while True:
attackEnemy = self.findNearestEnemy() # ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin()

I don’t know what’s the problem if any have the solution give me this please


#2

Could you format your code more accurately (use triple ` to wrap code) ? It will be easy to read your code and find a problem.


#3

Like Bryukh said, please format your question. Because we can not tell the indents of our code, and considering Python uses the indents it is important.


#4

You need to write the attackEnemy() function. This will be very similar to the pickUpCoin() function given in the default code:

def attackEnemy():
    # find the nearest enemy
    # if there is an enemy:
        # attack it!

When done, just uncomment the attackEnemy() line:

while True:
    attackEnemy()
    pickUpCoin()

#5

Sorry I newer in Python, so do you say me that I need to correct the code because you don’t understand the problem? With what?


#6

‘def findandattackenemy()’:
hero.findNearestEnemy()
‘if enemy’:
hero.attackEnemy

This is my function. The game say that i needed to find the nearest enemy and then if the enemy exist attack him but this function don’t respond.
The problem is that my hero begins to collect the money and then the enemy stands in front of my hero collecting the coins first. Do you have any function for me that it respond?


#7

I asked you to post your code as code, because now I don’t see indents and It’s hard to read you code.


#8

Peons are trying to steal your coins!

# Write a function to squash them before they can take your coins.

def pickUpCoin():
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)

# Write the attackEnemy function below.
# Find the nearest enemy and attack them if they exist!
enemy = hero.findNearestEnemy()
def attackEnemy():
    if enemy:
        hero.attack(enemy)

while True:
    attackEnemy() # ∆ Uncomment this line after you write an attackEnemy function.
    pickUpCoin()

thats my code idk what the problem is with the code it doesn't work the peons always get my coins

#9

and no i don’t know how to format my code


#10

@Joshua_Leon if you highlight the code and use the formatted text option it should display the code as code.


#11

ok i tried that and reposted it did it work?


#12

yes it did, great! I am looking at the code.


#13

oh, i moved the findNearestEnemy below def attackEnemy(): it fixed the problem


#14

You only find a single enemy at the very start of your code execution.

Read it out loud:

  1. You define pickUpCoin.
  2. You find the nearest enemy.
  3. You define attackEnemy.
  4. You start a while-true loop.
  5. You call attack enemy
  6. You check if enemy exists.
  7. You attack enemy.
  8. You call pick up coin.
  9. You find nearest coin.
  10. You check if coin exists.
  11. You move to the coin.
  12. Repeat (while-true loop)

Look at step #2.


A Fine Mint (python) ran out of time
#15
# Peons are trying to steal your coins!
# Write a function to squash them before they can take your coins.
def pickUpCoin():
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
# Write the attackEnemy function below.
# Find the nearest enemy and attack them if they exist!
def attackEnemy():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
while True:
    attackEnemy() # ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin()

is this right?
It won’t collect the coins?


#16

Please format your code with 3x ( ` ) before and after your code block, so I can see your indentation.


#17

I dont know how to do that sorry I am new?


#18

Be sure to read the part about “radiant, harmonious formatting”


#19

wow great service you cant help me !!! wow!


#20

I did it for you this time, but keep in mind in the future that improperly formatted Python code is hard to debug.

After formatting your code, I noticed that pickUpCoins isn’t indented. So it is never ran. Is this what you intended to do?