Skip to main content
 首页 » 编程设计

python之遍历字典并获取键

2025年05月04日10lexus

<分区>

我正在尝试获取字典中的名称及其对应的键值。 抱歉,如果已经有人问过这个问题。这段代码不起作用,因为我不擅长编程并且刚刚开始。请告诉我它有什么问题。

theBoard = {'top-L': ' ', 
'top-M': ' ', 
'top-R': ' ', 
'mid-L': ' ', 
'mid-M': ' ', 
'mid-R': ' ', 
'low-L': ' ', 
'low-M': ' ', 
'low-R': ' ' 
 
'Check for closed moves' 
def openMoves: 
    for i in theBoard: 
        if theBoard[i] == ' ': 
            print "the move %s is open" % theBoard[i] 
        else: 
            print "the move %s is taken" % theBoard[i] 
print openMoves()