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

Bookkeeper [Help Python] [Solved]

#1

I need help this code isn’t working and I can’t see how to fix it.

# 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.
if hero.time < 30:
    coin = hero.findNearestItem()
    
    while True:
        hero.moveXY(coin.pos.x, coin.pos.y)
        hero.gold = hero.gold + 1
if hero.time == 30:
    hero.moveXY(59, 33)
    hero.say(hero.gold)

# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)
#2

The while True loop for collecting coins needs to be rearranged. Take a look at the while True arrangement for the fightAndReport(). In that while True loop there are three components that you need. Your code has them just mixed around.

  1. Declare what you are looking for and assign a variable.
  2. Direct the hero what to do with what you found.
  3. Create a break using a time check. (needed to keep from an infinite loop)
#3

I do not see what you mean?

#4

Could I do this?

def collectAndReport(untilTime):
    collected = 0
    while True:
        item = hero.findNearestItem()
        if item:
            hero.moveXY(item.pos.x, item.pos.y)
            if item.type == "coin":
                collected += 1
        if hero.time > untilTime:
            break
    hero.moveXY(59, 33)
    hero.say(collected)
#5

I only need the report the coins part of this.

#6

Or could I do this?

def collectAndReport(Thirty):
    collected = 0
    Thrity = 30
    while True:
        item = hero.findNearestItem()
        hero.time = hero.time
        if item:
            hero.moveXY(item.pos.x, item.pos.y)
            if item <= 0:
                collected += 1
        if hero.time == Thirty:
            break
    hero.moveXY(59, 33)
    hero.say(collected)
#7

You can create a full function that mimics the fightAndReport(). Just make sure you call the function with the untilTime so that you run the function

fightAndReport(15) # how to call the function with the untilTime, write your function with the 30 secs

Also, instead of counting you can just use hero.gold to report your count, but this will work too.

    while True:
        item = hero.findNearestItem()
        if item:
            hero.moveXY(item.pos.x, item.pos.y)
            if item.type == "coin":    # don't need if you use hero.gold to report
                collected += 1   # don't need if you use hero.gold to report
        if hero.time > untilTime:  

#8

Where would I put hero.gold in this?

#9

When you use hero.say(hero.gold) at the end. I’m curious to see if your count is counting the number of coins instead of the value. Didn’t think of that at first.

I just checked, it looks like you need to report the value of the coins, not the count of coins. Using hero.gold is a little easier for this.