-1
我想寫一個基本的Hang子手遊戲,並且每當我嘗試執行它時,都會得到標題中提到的相同錯誤。我已經仔細研究了我的代碼,並且在解決了一些我注意到的問題的同時,我仍然遇到了同樣的錯誤。任何幫助?謝謝。掃描三引號字符串文字時Python - EOF
import random
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
o |
|
|
|
=========''', '''
+---+
| |
o |
| |
|
|
=========''', '''
+---+
| |
o |
/|\ |
|
|
=========''', '''
+---+
| |
o |
/|\ |
/ |
|
=========''', '''
+---+
| |
o |
/|\ |
/\ |
|
=========''', '''
話=「蟻狒狒獾蝙蝠熊狸駱駝貓蛤眼鏡蛇美洲獅狼烏鴉鹿狗驢鴨老鷹雪貂狐青蛙羊鵝鷹獅蜥駱駝摩爾猴駝鹿鼠標騾子蠑螈水獺貓頭鷹熊貓鸚鵡鴿蟒兔RAM老鼠烏鴉犀牛三文魚鯊魚密封羊臭鼬懶蛇蜘蛛鸛天鵝虎蟾蜍鱒魚火雞龜黃鼠狼鯨狼袋熊斑馬」 .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()
print('Missed letters:', end=' ')
for letter in missedLetters:
print(letter, end=' ')
print()
blanks = '_' * len(secretWord)
for i in range(len(secretWord)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
for letter in blanks:
print(letter, end=' ')
print()
def getGuess(alreadyGuessed):
while True:
print('Guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Please enter a single letter.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER.')
else:
return guess
def playAgain():
print('Do you want to play again? (yes or no)')
return input().1ower().startswith('y')
print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
while True:
displayBoard(-HANGMANPICS, missedLetters, correctLetters, secretWord)
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
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 have won!')
gameIsDone = True
else:
missedLetters = missedLetters + guess
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + 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
你的代碼是因爲壓痕的不理解,你可以添加適當的縮進代碼? – Jeril