I am trying out the hangman code from invent game with python book ,I am stuck on an invalid syntax error with 'end' .
Here is the traceback:
Line 82: print ('Missed letters: ', end = ' ')
Syntax error: Invalid syntax
arrow pointing at = sign. I searched and found that as I am working on python2.7 , I should use from future import print_function , which i did and error still exists.
My code:
import random
from __future_ import print_function
HANGMANPICS = ['''
+---+
| |
|
|
|
|
|
====== ''' , '''
+---+
| |
0 |
|
|
|
|
====== ''' , '''
+---+
| |
0 |
| |
|
|
|
====== ''' , '''
+---+
| |
0 |
/| |
|
|
|
======= ''' , '''
+----+
| |
0 |
/|\ |
|
|
|
======== ''' , '''
+----+
| |
0 |
/|\ |
/ |
|
|
======== ''' , '''
+------+
| |
0 |
/|\ |
/ \ |
|
|
========= ''']
words = """ cougar badger beaver cobra lion tiger skin ventriloquist magician monkey moose pigeon
rabbit rhino trout wombat kangaroo python lizard raven skunk peacock hoki crab prawns cancer
sloth snake spider parrot penguin ferret eagle cock hen peahen turkey turtle dinosaur metaphor
iteration object apple """.split()
def getRandomWord(wordList):
#this function returns a random string from the words list
wordIndex = random.randint(0,len(wordList)-1) #as we count from 0
return wordList[wordIndex]
def displayBoard(HANGMANPICS , missedLetters, correctLetters, secretWord):
print (HANGMANPICS[len(missedLetters)])
print() #give space after every string character
print ('Missed letters: ', end = ' ')
for letter in missedLetters:
print (letter, end = ' ')
print()
blanks = '_' * len(secretWord)
#replace blanks with correct guesses
for i in range (len(secretWord)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
#show secret word with spaces between each letter
for letter in blanks:
print (letter , end=' ')
print()
def getGuess(alreadyGuessed): #makes sure player enters a letter and nothing else
while True:
print ('Guess a letter.')
guess = raw_input()
guess = guess.lower()
if len(guess) != 1:
print "please enter a single character."
elif guess in alreadyGuessed:
print "you have already guessed that letter. Choose another."
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print "please only enter a letter"
else:
return guess #returns user i/p
#function is true if player wants to play again , else false
def playAgain():
print "do you wanna play again? (yes or no)"
return raw_input.lower().startswith('y')
print "<<<<<<<<<<< H A N G M A N >>>>>>>>>>>>"
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
while True:
displayBoard(HANGMANPICS , missedLetters , correctLetters , secretWord)
#getting a letter from user
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
#check if the player has won
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print "Yes! the secret word is " + secretWord + "! You win!"
gameisDone = True
else:
missedLetters = missedLetters + guess
#check if player has exhausted his guess limits and lost
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS , missedLetters , correctLetters , secretWord)
print " you have run out of guesses! \n After " + str(len(missedLetters)) + "missed guesses and" + str(len(correctLetters))
+ "correct guesses the word was " + secretWord
gameisDone = True
if gameisDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameisDone = False
secretWord = getRandomWord(words)
else:
break
Suggestions? Thanks!
random.choice(wordList)to give you a random word'\\') or use raw string literal (r'\ '). Otherwise the backslash will be used to escape whitespace after them.