2

I'm trying to get the names in a dictionary and their corresponding key values. Sorry if this has already been asked. This code is not working because I suck at programming and just starting. Please tell me what's wrong with it.

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
Matthew
  • 157
  • 1
  • 9
  • Well, you never close your dictionary, and to iterate over a dictionary use `for k, v in theBoard.items()` – bcorso Aug 03 '15 at 00:09
  • Next time, use Google to search for your question, and relevant answers on SO will appear at the top. Also, while you are composing your question, a list entitled **Questions that may already have your answer** shows up with potentially relevant questions. **Use that list** and open up the suggested questions in new tabs. This site has been around for over 6 years, if you suspect a question has already been asked, it probably has. Duplicates like this just waste time and effort. – MattDMo Aug 03 '15 at 00:21

2 Answers2

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
bcorso
  • 45,608
  • 10
  • 63
  • 75
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

I think your tabbing is off too...

Static
  • 1
  • 2