# Keeping time code problem [Solved]

hello, plz help me
I know this was solved but I tried all the solutions and it didn’t work.

# Utilise ton nouveau pouvoir pour choisir ce que tu dois faire : hero.time

while True:
# Durant les 10 premières secondes : se battre.
if hero.time < 10:

``````    if enemy:
enemy = hero.findNearestEnemy()
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)

pass
# Sinon, si on est toujours dans les 35 premières secondes, ramasser des Pièces.
elif hero.time < 35:
coin = hero.findNearestItem()
if coin :
hero.moveXY(coin.pos.x, coin.pos.y)
pass
# Après 35 secondes, rejoindre le raid !
else:
enemy = hero.findNearestEnemy()

if enemy:
enemy = hero.findNearestEnemy()
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.cleave(enemy)
pass``````

your repeating enemy = hero.findNearestEnemy() twice in your code near the end

``````enemy = hero.findNearestEnemy()

if enemy:
enemy = hero.findNearestEnemy()
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.cleave(enemy)
``````

You don’t need to move to the enemy’s position as calling `hero.attack()` will move you in range of attacking the enemy.

In the start of your code, you never define enemy, so you are checking if there is something that hasn’t been defined yet. You need to find the nearest enemy before you check if it exists and after you check if your hero’s time is less than the value 10. The next thing you need to fix is within your if loops checking for enemy, don’t define enemy again inside of the if loops. you only need to define enemy once within each fighting time period before checking if an enemy exists.

In this part above, you want to check if the hero is ready to cleave first, if he is ready, then cleave. Otherwise (else statement), you need to just attack an enemy.

yes that is a other thing that would make your coding easier but it is fine with it

I redid my entire code and i wanted to share it with you but by copying and pasting, there are no indentations .

thank u for the support anyway

Well, we’re actually not supposed to give our code if it works, because people can copy and paste it and pass the level. Also, Welcome to the forum!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.