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
  • 15,339
  • 4
  • 25
  • 45
A Display Name
  • 327
  • 8
  • 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,174
  • 11
  • 18
0

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
  • 8,605
  • 144
  • 77
  • 113
Travis
  • 105
  • 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