クラス

クラス#

Basic Exercises#

Basic-Q1#

name, ageの2つの属性を持つPersonクラスを定義せよ。info()メソッドを定義し、nameageを表示するようにせよ。

class Person:
    def __init__(self, name, age):
        # your code here

    def info(self):
        # your code here

alice = Person("Alice", 30)
alice.info()  # Name: Alice, Age: 30 と表示される

Basic-Q2#

Turtleクラスを定義せよ。Turtleクラスは、name, positionの2つの属性を持ち、move()メソッドを定義し、指定された上下左右の方向に1歩移動するようにせよ。positionの初期値は(0, 0)とする。

class Turtle:
    def __init__(self, name, position=(0, 0)):
        # your code here

    def move(self, direction):
        """
        Parameters
        ----------
        direction : str
            "up", "down", "left", "right"のいずれか
        """
        # your code here
        # 例: positionが(0, 0)のとき、"up"を指定するとpositionは(0, 1)になる
    
shelly = Turtle("Shelly")
print(shelly.position)  # (0, 0)

shelly.move("up")
print(shelly.position)  # (0, 1)

shelly.move("right")
print(shelly.position)  # (1, 1)