0

Python 3:来自“用 Python 发明游戏”的编码刽子手。语法 msg 说“无效语法”并突出显示打印功能。我认为问题在于刽子手单词列表和拆分方法。谢谢你的帮助。

import random
HANGMANPICS = ['''

  +---+
  |   |
      |
      |
      |
      |
      |
=========''', '''


  +---+
  |   |
  O   |
      |
      |
      |
      |
=========''', '''


  +---+
  |   |
  O   |
  |   |
      |
      |
      |
=========''', '''


  +---+
  |   |
  O   |
 /|   |
      |
      |
      |
=========''', '''


  +---+
  |   |
  O   |
 /|\  |
      |
      |
      |
 =========''', '''


  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
      |
=========''', '''


  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
      |
=========''']


words = '''ant baboon badger bat bear beaver camel cat clam
cobra cougar coyote crow deer dog donkey duck eagle ferret
fox frog goat gooose hawk lion lizard llama mole monkey
moose mouse mule newt otter owl panda parrot pigeon python
rabbit ram rat raven rhino salmon seal shark sheep skunk
sloth snake spider stork swan tiger toad trout turkey
turtle weasel whale wolf wombat zebra'''.split()



def getRandomWord (wordList):
    wordIndex = random.randint(0, len(wordList) -1)
    return wordList[wordIndex]


def displayBoard (HANGMANPICS, missedLetters, correctLetters, secretWord):
    print(HANGMANPICS[len(missedLetters)]
    print()           #this print statement is highlighted w/invalid syntax msg

    print('Missed letters:', end=' ')
    for letter in missedLetters:
    print(letter, end=' ')
    print()

    blanks = '_' * len(secretWord)

for i in range(len(secretWord)):    #replaces blanks w/correctly guessed ltrs
    if secretWord[i] in correctLetters:
        blanks = blanks[:i] + secretWord[i] + blanks[i + 1:]

for letter in blanks:               #show secret word w/spaces between ltrs
    print(letter, end=' ')

print()
4

1 回答 1

2

您忘记了在刚才的那一行之前的右括号print()

print(HANGMANPICS[len(missedLetters)]  # Note the missing `)` here

当您在 Python 中遇到无法解释的语法错误时,请检查一行;Python 期望在那里找到print()函数的更多参数,而下一行违反了这一期望。

于 2013-01-12T15:23:06.603 回答