2013-12-10 22 views
-2

Python字典一直困扰着我。如何在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** 
+0

我创建了词典......这是我不知道该怎么办的实现。我知道他们用于什么..这是他们如何使用,我不知道该怎么做。 – juice

+0

我只是问最后一行是否正确使用它。 – juice

回答

3

我相信你正在寻找的是:

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

感谢您的帮助 – juice

相关问题