I have been stuck on this for awhile.I dont know whats wrong can someone help pls
while True:
enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex<len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != "sand-yak" and enemy.health > 0:
hero.attack(enemy)
enemyIndex += 1
pass
hero.moveXY(40, 32)
It has to do with indentations. The âwhile enemyIndexâ loop, needs to be indented, so as to make it an inner loop, or a child loop (member), of the outer âwhile trueâ loop.
- Just to explain, the terms âinner loopâ and âchild loopâ are often used interchangeably. They both imply that there is another loop in the hiarchy (above) that they are working for.
- This other loop is referred to either the âparent loopâ, or the âouter loopââŚall of these terms are correct, itâs just a matter of preference, how you learned, etc.
You will also have at least one other issue, but letâs address that, once you get this one fixedâŚa bit less confusing that way.
Yep, that looks pretty good. And, you fixed the other issue alreadyâŚenemyIndex += 1 is properly aligned.
Now, think about when you want to moveâŚ
My person hits one person then stops with a red x under him?
As shown above, your moveXY statement is aligned with âwhile trueââŚthat means that it is on the same hierarchical level as the while statementâŚit also means it will never execute. Instead, it needs to be a member of the while loop, not a peerâŚit needs to be the final statement of the loop.
wait sorry when i was fixing code i deleted a âoâ in hero my bad
hehâŚbeen there myself. Does the level pass now? If it does, please edit the posts above (except the first one) and remove your code. We donât want to post solutions, as someone may find that post, copy and paste your code and pass the level without having learned a thing.
1 Like
okay thanks for all the help
1 Like
This is my code. It seems fine, but i keep dying
Iâm in a class, so i canât get better armor or weapons
while True:
enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.type != "sand-yak":
if enemy.health > 0:
hero.attack(enemy)
enemyIndex += 1
pass
Howdy and welcome to the forum!
With this code, you are testing to see that the enemy is not a yakâŚgood! Next, you test to see if its health is greater than zeroâŚok, but not what you are looking for. I strongly suggest that you donât delete the comments, as they tend to help guide you and keep you on track:
# While the enemy's health is greater than 0, attack it!
Oh ok, thanks!
I thought it was because i had not alot of health or something
1 Like