# Ogre Gorge Gouger (Python)[SOLVED]

Hi,

For some reason i am getting an infinite loop and I cant work out why.
Surely the loop should end when the time == 20?

here’s the code and equipment.

``````# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins

coins = hero.findNearest(hero.findItems())
coin = coins

if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)

``````

Many Thanks

hi, no it wasnt.

the full code is here.

``````# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins

coins = hero.findNearest(hero.findItems())
coin = coins

if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)

while hero.pos.x > 16:
# Retreat behind the fence
hero.say("I should retreat")
hero.move({"x" : 17 , "y" : 38 })

# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)

``````

change it to if the precode is wrong.

which `while` am i changing?
i tried the top one and the hero then cant decide what to do.
if i change the bottom one he just builds the fence before picking up coins.

change the second and delete the say message.

Still has an infinite loop.

``````# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins

coins = hero.findNearest(hero.findItems())
coin = coins

if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)

if hero.pos.x > 16:
# Retreat behind the fence

hero.move({"x" : 17 , "y" : 38 })

# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)
``````

change that to

``````coin = hero.findNearestItem()
``````

change that to

``````if coin:
``````
1 Like

Ok so the hero now picks coins up but for some reason he wont move behind the barrier before building the fence in the gap?

``````# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)

if hero.pos.x > 16:
# Retreat behind the fence

hero.move({"x" : 15 , "y" : 36 })

# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)

``````

i’ve had another look at it and it the `hero.move({"x" : 15 , "y" : 36 })` that wont work.
it just ignores my code.

``````pos = {"x" : 15 , "y" : 38 }
while  hero.pos.x > 16: # while instead of if
hero.jumpTo(pos)
else:
hero.move(pos)
``````

make a function moveAndJump(pos) following the same pattern and use it also to collect coins.
What is your main language javascript or python?

2 Likes

@xython
hi xython,
my language is python.

thanks for the tip. i have passed the level but i am still trying to get the 60 gold. (my record is 59 so far).

thanks

1 Like

So, since you have solved it would it be ok if I added [Solved] to the title?

1 Like

@098765432123
Yeah sure thing.

I got the 60 gold btw using minimal changes to the base code (i gave myself a little extra time).

It was cheeky but it worked.

Thanks for the help everyone.
This forum is awesome!

2 Likes

hey can you click the checkbox on the post that helped the most?

1 Like