Reputation: 43
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!
Upvotes: 2
Views: 2229
Reputation: 369384
There's a typo:
from __future_ import print_function
^-- single underscore
__future_
should be replaced with __future__
(two trailing underscores)
And the import statement should be at the first line of the file (before any other import
s)
from __future__ import print_function # <-- this should be the first
import random
In addition to that, all usages of print
statement should be replaced with print
function:
Statements like:
print "please enter a single character."
should be replaced with:
print("please enter a single character.")
Upvotes: 1
Reputation: 172
first, you seem to be importing from
__future_
you should import from
__future__
(note last underscore)
second, this replaces the normal print with an actual print function, so instead of
print "your text here"
it should be
print("your text here")
gameisDone is not always initialized. add gameisDone=false after
if foundAllLetters:
print "Yes! the secret word is " + secretWord + "! You win!"
gameisDone = True
else:
raw_input IS NOT A STRING. it's a function. call it to get a string instead of getting the function. Like here:
return raw_input.lower().startswith('y')
should be
return raw_input().lower().startswith('y')
also you need to import random.
I ran your code with fixes and it worked.
Upvotes: 1
Reputation: 5297
print()
in this context is the print function in Python 2.7. As parentheses are optional for the print built-in statement, print
statements can resemble print()
function calls. Your print()
function call appeared to follow proper syntax, which told me it was likely a problem with the import
(such problems could also indicate a version issue).
You attempted to import the function with:
from __future_ import print_function
But you are missing a '_'. It should be
from __future__ import print_function
I reproduced this bug in the command line with your import and with other prints.
bash-4.1$ python
Python 2.7.8 (default, Jul 25 2014, 14:04:36)
[GCC 4.8.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print (letter , end=' ')
File "<stdin>", line 1
print (letter , end=' ')
^
SyntaxError: invalid syntax
>>> print ('a', end=' ')
File "<stdin>", line 1
print ('a', end=' ')
^
SyntaxError: invalid syntax
>>> print('a', end=' ')
File "<stdin>", line 1
print('a', end=' ')
^
SyntaxError: invalid syntax
>>> from __future__ import print_function
>>> print('a', end=' ')
a >>>
Upvotes: 1