# Oasis - Desert Coding Help

#1

So I am currently on the level Oasis in the Desert world but I can’t figure out the code!
Here is the code that I entered:

``````loop:
enemy = self.findNearestEnemy()
if enemy and self.distanceTo(enemy) < 10:
# Move to the left by subtracting 10 from your X coordinate.
pass
x = self.pos.x - 10
y = self.pos.y
self.moveXY(x, y)
else:
# Move to the right by adding 10 to your X coordinate.
pass
x = self.pos.x + 10
y = self.pos.y
self.moveXY(x, y)
``````

I can’t figure out what I am doing wrong so if anyone could help me out that would be nice…

1 Like
#2

I don’t see any problem with your code except indentation. I used your code (with proper indentation) and passed the level easily. Are you sure that your code has the right indentation ? Code indentation is important in Python.

1 Like
#3

The indentation is missing because the code is not presented properly. It is in a quote-block, but should be in a code-block.

Hi PizzaCat and welcome to the forum,
you can present your code properly by putting three backticks (```) before and behind it. You can also read this in the FAQ, which you should have read anyway before posting.
As newcomer-bonus, I did this for you this time.

1 Like
#4

***Note: ignore the following advice as it’s wrong… mb ***

The issue are the two “pass” lines – pass will jump you out of a loop. So each if statement ends as soon as it hits a “pass”

1 Like
#5
``````loop:
x = 3
if x == 3:
pass
self.say("pass")
``````

“says” pass just fine…

pass is just a nop (no operation) placeholder stub
used because:

``````if x==3:
else:
``````

is unhappy “code”

1 Like
#6

Oh, got it. I don’t know where I picked up the idea that “pass” jumped you out of the loop.

In that case, I don’t see anything wrong with the code.

1 Like
#7

What you mean is `break`, which is currently not teached.

1 Like
#8

(tl;dr code is OK, just try re-submitting it)

Hmmm… they must be playing around with parameters behind the scenes. I think the code above is fine (essentially the same as what I have). I just submitted and got killed, but then resubmitted and was fine.

I think these sorts of levels can be really, really sensitive to the parameters because they depend on speed and timing of everyone (e.g. speed of heroes, boots worn). In particular, on this level the code we are asked to write is really very naive with respect to the yak movement. It only happens to work if the yaks appear in a specifically choreographed (and non-random) way. The code to actually deal with randomized yaks here would be much more complicated than what we are asked to do here.

1 Like
#9

pass -> does nothing, just a placeholder
break -> exit loop completely

I’m surprised these aren’t taught as they aren’t really all that complicated and in many cases lead to much simpler and more readable code than if you don’t know about them. But I’m not much of a teacher, so…

I will say that I think pass ought to be explained somewhere (I don’t think it is?) because a lot of people leave it in the code when it’s not necessary, presumably b/c they don’t realize it’s just in the sample code as a placeholder. Sort of drives me a little crazy b/c I hate unnecessary code artifacts cluttering things up.

1 Like
#10
``````loop:
enemy = self.findNearestEnemy()
if enemy and self.distanceTo(enemy) < 10:
# Move to the left by subtracting 10 from your X coordinate.
pass
x = self.pos.x - 10
y = self.pos.y
self.moveXY(x, y)
else:
# Move to the right by adding 10 to your X coordinate.
pass
x = self.pos.x + 10
y = self.pos.y
self.moveXY(x, y)
``````

i think i did it right now in the code block

1 Like
#11

Yes. As noticed by eiler13:

You don’t need them.

1 Like
#12

Excuse me, what is wrong with my code?
while True:
x = hero.pos.x
y = hero.pos.y
enemy = hero.findNearestEnemy()
if enemy and hero.distanceTo(enemy) < 10:
# Subtract 10 from x to move left.
newX = hero.pos.x - 10
# Use moveXY to move to the new x, y position.
hero.moveXY(newX, y)
pass
else:
# Add 10 to x to move right.
newX = hero.pos.x + 10
# Use moveXY to move to the new x, y position.
hero.moveXY(newX, y)
pass

1 Like
#13

Your code work fine, it must be a problem with your indentation.
Use the </> buttons and paste your code inside the dots so we can see the indentation.

1 Like
#14

thanks, i finished it