Python 3:
Coding hangman from “Invent Games with Python.” Syntax msg says “invalid syntax” and highlights print function. I think problem is with the hangman word list and split method. Thanks for your help.
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()
You forgot a closing parenthesis on the line before the one with just
print()on it:When you get a unexplained syntax error in Python, check the preceding line; Python expected to find more arguments to the
print()function there, and the next line violated that expectation.