Keeping Time help plz

# Use your new skill to choose what to do: hero.time

while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 10:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time < 35:
        item = hero.findNearestItem()
        if item.type == "coin" and item.value == 2:
            hero.moveXY(item.pos.x, item.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass

I need help :worried::worried::worried::worried::worried:

Delete the if statement:
if item.type == "coin" and item.value == 2

you just need to delete and item.value == 2

Alexander_gu has already said that.

dont give out answers it doesnt let them learn

I forgot about CodeCombat and have never finished Keeping Time, I am still stuck

# Use your new skill to choose what to do: hero.time

while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 10:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time < 35:
        item = hero.findNearestItem()
        if and item.value == 2
            hero.moveXY(item.pos.x, item.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass

This one needs adjustment. You should add a column at the end of the line and there should be something in between the if and the and.

It should look like this: if item and item.value == 2:

this doesn’t work

# Use your new skill to choose what to do: hero. time

while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 10:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time < 35:
        item = hero.findNearestItem()
        if item and item.value == 2 :
            hero.moveXY(item.pos.x, item.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass


Maybe try elif hero.time > 10 and hero.time < 35:

Nope :slightly_frowning_face:

# Use your new skill to choose what to do: hero.time

while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 10:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time > 10 and hero.time < 35:
        item = hero.findNearestItem()
        if item and item.value == 2 :
            hero.moveXY(item.pos.x, item.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        hero.attack(enemy)
        pass

item, which calls the hero.findNearestItem() function, returns the nearest item. If the nearest item is not a silver coin, the hero will do nothing. That being said, there are two apparent solutions to the problem. First, you could remove the item.value == 2 condition. Or, you could use a for-loop (or a while-loop) to iterate over the array of items and move to any item in the array with a value of 2.

2 Likes

my hero moves to the coins but he dies

this level makes my head hurt :frowning_face:

Use the same code as above (that’s the second code you posted) but define what enemy is and check if it exists in between these two lines:

while True:
    # If it's the first 10 seconds, attack.
    if hero.time < 5:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass
    # Else, if it's the first 35 seconds, collect coins.
    elif hero.time < 40:
        item = hero.findNearestItem()
        if item and item.value == 1 :
            hero.moveXY(item.pos.x, item.pos.y)
        pass
    # After 35 seconds, attack again!
    else:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)
        pass


It says this but I already have if in the code. This code is in the else spot
What do I do to fix this?

I go the answer on https://discourse.codecombat.com/t/solved-keeping-time/15572/7
:smiley::smiley::smiley::smiley::smiley::smiley: :smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley::smiley:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.