Computer scienceProgramming languagesPythonObject-oriented programmingMethods

Methods and attributes

Turtle

Report a typo

Here's a class Turtle that represents a turtle that is moving in the 2D world. The turtle can move in four directions: up, down, left and right. The turtle can make n steps at a time and the only restrictions are that x0,y0x \geq 0, y \geq 0.

class Turtle:
    def __init__(self, x, y):
        # the initial coordinates of the turtle
        self.x = x
        self.y = y

    def move_up(self, n):
        self.y += n

    def move_down(self, n):
        self.y = 0 if n > self.y else self.y - n

    def move_right(self, n):
        self.x += n

    def move_left(self, n):
        self.x = 0 if n > self.x else self.x - n

What will be the coordinates of the Turtle leo after these movements? Choose the pair (x, y).

leo = Turtle(1, 1)
leo.move_up(7)
leo.move_left(5)
leo.move_down(4)
leo.move_right(6)
Select one option from the list
___

Create a free account to access the full topic