Python: while loop does not work on another level

Hello codePros,

I am not sure why the same logic does not work on one level that worked before on a previous one. The code that worked:

while-loop with increment

The code that did not work on level mirage maker:

# Lure the ogres into an ambush!

# While your gold is less than 25, collect coins.
hero.gold = 0
while hero.gold < 25:
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    hero.gold += 1
    
# After the while loop, build a "decoy" at the white X.
hero.buildXY("decoy", 72, 68)
# While your health equals maxHealth, say insults
while hero.health == hero.maxHealth:
    hero.say("Your mom slept with lizards.")
    hero.say("That stone next to you is more clever than you")
    hero.say("A bug can bug me more than you")


# Then move back to the red X.
hero.moveXY(22, 16)

Error:

I tried to use the same logic and I am not sure what went wrong. The FAQ of this forum does not mention anything about protected property and google failed me on this one. Any idea?

Hi!
Try to change hero.gold to any other variable, like Total Gold f.e.

1 Like

it should be hero.gold according to the game:

However I tried your suggestion and that error says β€œtotal not defined”.
It really worked in every previous case I can recall.

Try doing:

total = hero.gold

or something like that?

You may not have an item that can do .gold
If you’re not sure, you can send a screenshot of your armor.

Mhm. I think you need quartz sense stone and above?

2 Likes

Oh, I see. So I guess @CocoCharlie and @thebagel may be right.
P.S.

In game documentation example you may β€œlook” how much gold does your hero have. Your code hero.gold += 1 tries to change hero.gold amount. It’s like hero.health=hero.health*100 it won’t work, protected property alert displays in this case.
Again, looks like you need special item and total = hero.gold variable.

1 Like

Yup, it’s quartz sense stone or above. Just checked.

3 Likes

Or simply delete the lines ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ and ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
and the working code will be
ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
but there is something your mother will not like :slight_smile: :
ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
It’s better simply to stay still until the ogres approach and then run ( sorry, javascript, but the idea is clear)

hero.buildXY("decoy", 72, 70);
// While your health equals maxHealth, say insults No, No, No - I'm a good boy/girl!
while (true){
    var enemy = hero.findNearestEnemy();
    // if ( hero.health == hero.maxHealth) is the way the help tells you but is little bit dangerous
    if ( hero.distanceTo(enemy) > 10)
       hero.moveXY(hero.pos.x, hero.pos.y);
     else break;
}
4 Likes

@xython : this is indeed what worked.

To have the full picture: I had the quartz stone but bought one the next level of necklace also to see whether it’s just a game bug but got the same error. When I removed those two values, it worked like a charm.
@Alexbrand , you made me think about what you wrote, that explains at least half of the problem, thank you for the idea.

TL,DR: All in all, thank you for all the support, I can move forward for a better understanding how coding works!

1 Like

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