4

I want to make the turtle goto the floor if it is above a set of coordinates:

something like this:

floor = -323

if turtle above floor:
    turtle.goto(floor)

But I don't know how the 'if' statement would work, as you can't simply put 'if turtle above floor' Any help?

kederrac
  • 16,819
  • 6
  • 32
  • 55
A Display Name
  • 367
  • 1
  • 9
  • 18

2 Answers2

3

Assuming that your "floor" is at y=-323 you could probably do something like this:

floor = -323

if turtle.ycor() > floor:
     turtle.sety(floor)

You retrieve the turtles y coordinate with turtle.ycor(), check if its larger than floor, and if it is set the y coordinate equal to the floor.

user5219763
  • 1,284
  • 12
  • 19
1

I would add the X coordinate of the turtles x, too, just so it doesn't respond with an error.

floor = -323
if turtle.ycor() > floor:
    turtle.goto(turtle.xcor(), floor)
Pang
  • 9,564
  • 146
  • 81
  • 122
Travis
  • 163
  • 1
  • 3
  • 17
  • 1
    What error do you anticipate `sety()` throwing different from `goto()`? Just curious. – cdlane May 24 '17 at 07:08
  • 1
    I've had some weird things happen, so I use goto, and it is reliable. Might've been operator error though. – Travis May 25 '17 at 01:13