如何正确使用Python中的字典

时间:2013-12-10 02:00:17

标签: python class python-2.7 dictionary

Python词典总是让我感到困惑。

我有3个词典:

left_dict = {"N":"W", "W":"S", "S":"E", "E":"N"}
right_dict = {"N":"E", "E":"S", "S":"W", "W":"N"}
turn_dict = {"N":"S", "E":"W", "S":"N", "W":"E"}

我有一个类机器人,它被初始化为:

class Robot:
    def __init__(self, team, x, y, direction):
        self.team = team
        self.health = 50
        self.x = x     
        self.y = y
        self.direction = direction

在这个课程中,我有一个改变方向的方法。

def left90(self):
        self.direction = left_dict <---- **is this how I would change the direction**

1 个答案:

答案 0 :(得分:3)

我相信你要找的是:

def left90(self):
    self.direction = left_dict[self.direction]