我正在尝试在字典中获取名称及其相应的键值。 对不起,如果已经问过这个问题。这段代码不起作用,因为我吮吸编程并刚开始。请告诉我它有什么问题。
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()
答案 0 :(得分:1)
theBoard = {'top-L': ' ',
'top-M': ' ',
'top-R': ' ',
'mid-L': ' ',
'mid-M': ' ',
'mid-R': ' ',
'low-L': ' ',
'low-M': ' ',
'low-R': ' '
} # <--- Close your dictionary
# <--- remove random string 'Check for c...'
def openMoves(): # <--- add parenthesis to function
for k, v in theBoard.items(): # <--- loop over the key, value pairs
if v == ' ':
print "the move %s is open" % k
else:
print "the move %s is taken" % k
openMoves() # <-- remove the print statement
答案 1 :(得分:0)
theBoard = {'top-L': ' ',
'top-M': ' ',
'top-R': ' ',
'mid-L': ' ',
'mid-M': ' ',
'mid-R': ' ',
'low-L': ' ',
'low-M': ' ',
'low-R': ' '}
def openMoves():
for k,v in theBoard.items():
if v == ' ':
print "the move %s is open" %k
else:
print "the move %s is taken" %k
我认为你的标签也已关闭......