gpt4 book ai didi

'end' 参数的 Python 无效语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:52:55 24 4
gpt4 key购买 nike

我正在使用 python 书尝试 invent game 中的 hangman 代码,我被“end”的无效语法错误困住了。

Here is the traceback: 
Line 82: print ('Missed letters: ', end = ' ')
Syntax error: Invalid syntax

箭头指向 = 符号。我搜索并发现,当我在 python2.7 上工作时,我应该使用 from future import print_function ,我这样做了,但错误仍然存​​在。

我的代码:

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

建议? 谢谢!

最佳答案

有一个错字:

from __future_ import print_function
^-- single underscore

__future_ 应替换为 __future__(两个尾部下划线)


并且 import 语句应该在文件的第一行(在任何其他 import 之前)

from __future__ import print_function  # <-- this should be the first
import random

除此之外,所有print 语句的用法都应替换为print 函数:

语句如下:

print "please enter a single character."

应该替换为:

print("please enter a single character.")

关于 'end' 参数的 Python 无效语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com