Hi, I have been trying to pass this level and can’t pass it. Can you help me?
Here’s my code:
# Fight enemies for 15 seconds.
# Keep count whenever an enemy is defeated.
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say(defeated)
# Collect coins until the clock reaches 30 seconds.
item = hero.findNearestItem()
# Tell Naria how much gold you collected.
while True:
hero.moveXY(58, 33)
hero.gold
hero.say("collected")
# Fight enemies until the clock reaches 45 seconds.
# Remember to reset the count of defeated enemies!
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
# Tell Naria how many enemies you defeated.
while True:
pet.moveXY(59, 33)
hero.say("defeated")
All you need to do is implement the structure of you first while loop into the following item = hero.findNearestItem statement and while loop. The level asks you to fight for 15 seconds, which you’ve done with your
if hero.time > 15:
break
statement.
It then requires you to collect gold for fifteen seconds. However you’ve only written item = hero.findNearestItem. If you repeat you first loop, but adapt it to deal with the task of collecting coins, and stopping when the clock reaches 30 seconds, that should work fine.
Finally you need to do the same thing for your last while loop, but, again, adapted to fight enemies, and stop when the clock reaches 45 seconds.
also why didn’t you just start with the default?
like this:
# This function allows to fight until the certain time
# and report about defeated enemies.
def fightAndReport(untilTime):
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)
# Collect coins until the clock reaches 30 seconds.
# Tell Naria how much gold you collected.
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)
I’m not sure if I fixed it correctly, let me know.
Here’s my code:
# Fight enemies for 15 seconds.
# Keep count whenever an enemy is defeated.
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say(defeated)
# Collect coins until the clock reaches 30 seconds.
while True:
item = hero.findNearestItem()
# Tell Naria how much gold you collected.
while True:
hero.moveXY(59, 33)
hero.say("hero.gold")
def fightAndReport(untilTime):
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)
# Collect coins until the clock reaches 30 seconds.
findAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)
# Fight enemies for 15 seconds.
# Keep count whenever an enemy is defeated.
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say(defeated)
# Collect coins until the clock reaches 30 seconds.
while True:
item = hero.findNearestItem()
if item:
pos = item.pos
x = pos.x
y = pos.y
# Tell Naria how much gold you collected.
while True:
hero.moveXY(59, 33)
hero.say(hero.gold)
def fightAndReport(untilTime):
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)
# Collect coins until the clock reaches 30 seconds.
findAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)
You have a successful pattern, just follow it:
#################################
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break
hero.moveXY(59, 33)
hero.say(defeated)
###################################
while True:
# your code
y = pos.y
# Where do you collect coins?
# Where do you exit the loop?
Then you tell Naria how much gold you collected but why in a “while True” loop? See how you did it when fighting enemies.
Then your function fightAndReport isn’t needed. Simply follow the initial pattern while not forgetting to zero the defeated enemies and to change the hero.time
right now, you would be standing in 59, 33 staring at Naria and saying quietly That coin is over there, that one is there, a diamond is right there, and NO! an ogre! but How do I deal with it… anyway, that coin is there, that one is there…
# Fight enemies for 15 seconds.
# Keep count whenever an enemy is defeated.
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > 15:
break
# Tell Naria how many enemies you defeated.
hero.moveXY(59, 33)
hero.say(defeated)
# Collect coins until the clock reaches 30 seconds.
item = hero.findNearestItem()
# Tell Naria how much gold you collected.
while True:
hero.moveXY(58, 33)
hero.gold
hero.say("collected")
# Fight enemies until the clock reaches 45 seconds.
# Remember to reset the count of defeated enemies!
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
# Tell Naria how many enemies you defeated.
while True:
pet.moveXY(59, 33)
hero.say("defeated")
Let’s start with this. This isn’t really doing anything at the moment. You’re supposed to be collecting coins. Try copying the first section of code and replacing bits about enemies to items, and instead of incrementing defeated you can leave that bit out and use hero.gold, as you seem to have tried to do there.
Remember you must always break from a while loop or it will continue for ever, the code below the section I quoted will never.
Do the same for the next enemy section.