Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Bookkeeper Help!

My code is correct but every time I pass over a coin that isn’t labeled “item” it doesn’t count toward my “totalGold”… how do I fix that??

# 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.
def collectCoins(untilTime):
    totalGold = 0
    while True:
        item = hero.findNearestItem()
        if item:
            hero.moveXY(item.pos.x, item.pos.y)
            totalGold += item.value
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(totalGold)

collectCoins(30)

fightAndReport(45)

![44%20AM|690x387](upload://xArPn2Stb1X3kWSQwc1p0Zj2BG.png)

i think it maybe be that u need to define both
else IDK

:frowning_face: :frowning_face: I got it, but not in submit so it didn’t count!!

define both of what though?

item/coin

(20 doritos)

Hi @CranKeD_Wired,
the problem here is that you use a totalGold variable. In theory it works, but in practice what happens is that your hero collects two coins at once, and because of the way your code works it thinks it’s only collected one. Hence the wrong number at the end. Instead you can just the built in gold variable :wink:.
By the way I really like your code, well done for using functions and the totalGold variable. It’s very neat. It’s just that the variable doesn’t work in this specific level.
I hope this solves your problem,
Danny

1 Like

so instead of doing totalGold = 0 and totalGold += item.value, I should do hero.gold = 0 and hero.gold += item.value? Also, thank you for the compliments!

oh I got it! Thank you sm!!

1 Like