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()
答案 0 :(得分:2)
在之前的行上忘记了一个右括号上面只有print()
的那个:
print(HANGMANPICS[len(missedLetters)] # Note the missing `)` here
当您在Python中遇到无法解释的语法错误时,请检查前面的行; Python期望在那里找到print()
函数的更多参数,并且下一行违反了该期望。