2013-12-10 19 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

相關問題