# 1. 设置秘密单词
secretWord = "cattle"
# 2. 初始化空白列表
def initializeBlanks(secret):
blanks = ["#"] * len(secret)
return blanks
# 3. 填充空白列表
def fillInBlanks(secret, blanks, guessedLetter):
for i in range(len(secret)):
if secret[ i] == guessedLetter:
blanks[ i] = guessedLetter
return blanks
# 4. 猜字母
def getGuess(wrongGuessList):
guess = input("Guess a letter: ").lower()
if guess in wrongGuessList:
print("You already guessed that letter.")
return getGuess(wrongGuessList)
else:
return guess
# 5. 游戏流程
def playGame(secret):
blanks = initializeBlanks(secret)
wrongGuessList = []
wrongGuessCount = 0
maxWrongGuessCount = 5
while "#" in blanks and wrongGuessCount < maxWrongGuessCount:
guessedLetter = getGuess(wrongGuessList)
if guessedLetter in secret:
blanks = fillInBlanks(secret, blanks, guessedLetter)
else:
wrongGuessList.append(guessedLetter)
wrongGuessCount += 1
print("Wrong guess! You have {} guesses left.".format(maxWrongGuessCount - wrongGuessCount))
statusReport(blanks, wrongGuessList)
if "#" not in blanks:
print("Congratulations! You guessed the word: {}".format(secret))
else:
print("Sorry, you didn't guess the word. The word was: {}".format(secret))
# 6. 输出游戏状态
def statusReport(blanks, wrongGuessList):
print("Current status:")
print("Blanks: ", " ".join(blanks))
print("Wrong guesses: ", " ".join(wrongGuessList))
# 7. 画雪人
def display(wrongGuessCount):
snowman = [
[" ", "O", " "],
["/", "|", "\"],
[" ", "|", " "],
["/", " ", "\"]
]
for i in range(wrongGuessCount):
print(" ")
for row in snowman:
print(" ".join(row))
# 8. 游戏开始
playGame(secretWord)
display(wrongGuessCount)