Why is my code not working?
while True:
enemy = hero.findNearestEnemy()
if enemy.type != 'sand yak':
if enemy.type != 'burl':
hero.attack(enemy)
else:
break
hero.moveXY(57, 34)
while True:
if hero.gold <= 20:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
else:
break
hero.moveXY(78, 34)
it keeps on saying this :
Aya
December 16, 2022, 10:17am
2
Before these lines and an if statement checking if there is an enemy in the first place.
Hint:
It keeps on collecting coins and I have to defeat 7 ogres, is there anything else I should change?
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != 'sand yak':
hero.attack(enemy)
if enemy.type != 'burl':
hero.attack(enemy)
else:
break
hero.moveXY(57, 34)
while True:
if hero.gold <= 20:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
else:
break
hero.moveXY(78, 34)
Learned
December 16, 2022, 2:50pm
4
If your code does not initially detect an enemy (which is what I assume is happening), it will move to the second While True loop where you collect coins.
another problem is this code
because if the enemy is a burl it will atack the burl before realizing that is a bad idead hint elif:
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != "sand yak":
hero.attack(enemy)
elif enemy.type != "burl":
hero.attack(enemy)
else:
break
hero.moveXY(57, 34)
while True:
if hero.gold <= 20:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
else:
continue
hero.moveXY(78, 34)
like this?
what does “burn 20 chars” mean?
it is to fill in when the discouce won’t let ypu post because you don’t have 20 charecters
My character keeps on collecting coins, but it says I have to defeat 7 ogres.
remove the secoind while true and instead of else break say else and the your coin colecting code
It went straight to the red x when I did that, do I have to use "item = herofindNearestItem() on top of the else?
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != "sand yak":
hero.attack(enemy)
elif enemy.type != "burl":
hero.attack(enemy)
else:
break
hero.moveXY(57, 34)
if hero.gold <= 20:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
else:
hero.moveXY(item.pos.x, item.pos.y)
hero.moveXY(78, 34)
I changed my code a little, it kills some ogres but not all of them, is anything else wrong?
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != "sand yak":
hero.attack(enemy)
elif enemy.type != "burl":
hero.attack(enemy)
else:
continue
hero.moveXY(35, 34)
hero.moveXY(57, 34)
if hero.gold <= 20:
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
else:
hero.moveXY(item.pos.x, item.pos.y)
hero.moveXY(78, 34)
if enemy.type != "sand yak":
hero.attack(enemy)
elif enemy.type != "burl":
hero.attack(enemy)
This would cause you to attack burls sometimes, it should be enemy type is not sand yak AND enemy type is not burl
Is there anything else wrong with my code? (It doesn’t attack the required number of ogres, but I have to defeat 7)
Can you provide a link to the level?
Hm the link isn’t working for me. Is this part of a class or something?
yes it is, but where do I get a normal link?
I don’t think i can access those types of levels because I’m not in a class or anything for them. Sorry