代码拉取完成,页面将自动刷新
# 火柴人猜单词游戏
import random
# ASCII码构成的图片资源
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\\ |
|
|
=========''', '''
+---+
| |
O |
/|\\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\\ |
/ \\ |
|
=========''']
# words 的 字典方式进行增加单词丰富性
words = {'Colors': '''red orange yellow green blue indigo violet white black
brown'''.split(),
'Shapes': '''square triangle rectangle circle ellipse rhombus trapezoid
chevron pentagon hexagon septagon octagon'''.split(),
'Fruits': '''apple orange lemon lime pear watermelon grape grapefruit cherry
banana cantaloupe mango strawberry tomato'''.split(),
'Animals': '''bat bear beaver cat cougar crab deer dog donkey duck eagle fish
frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit
rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat
zebra'''.split()}
# 随机生成一个单词
def getRandomWord():
style = random.choice(list(words.keys()))
index = random.randint(0, len(words) - 1)
return [style,words[style][index]]
# 展示游戏面板:显示当前所猜到的字母
def showPlayBroad(errorLetter, correctLetter, guessWord):
# 最多七次机会去猜对答案
if len(errorLetter) <= len(HANGMANPICS)-1:
print(HANGMANPICS[len(errorLetter)])
# 打印错误的字母
print("ERROR LETTER:", end=' ')
for i in range(len(errorLetter)):
print(errorLetter[i], end=' ')
print()
blank = '_' * len(guessWord)
# 打印现在已经猜的代码
print("Take a guess")
for i in range(len(guessWord)):
if guessWord[i] in correctLetter:
# 刷新blank游戏面板
blank = blank[:i] + guessWord[i] + blank[i + 1:]
print(blank)
# 获取输入的字母
def getGuessLetter(alreadyGuess):
while True:
print("Guess:", end=" ")
guess = input().lower()
# 条件判断只能输入
if len(guess) > 1:
print("Please enter a letter!")
elif guess not in "qwertyuioplkjhgfdsazxcvbnm":
print("It is not English LETTER!")
elif guess in alreadyGuess:
print("You have guessed this letter")
else:
return guess
# 是否在玩一次
def isPlayAgain():
print("Would u play again?")
answer = input().lower()
return answer.startswith('y')
"""
正式开始游戏
"""
if __name__ == '__main__':
# 多返回,对应下标
secretStyle,secretWord = getRandomWord()
alreadyGuess = ''
errorGuess = ''
correctGuess = ''
while True:
print("the style is {}".format(secretStyle))
# 展示画板
showPlayBroad(errorGuess, correctGuess, secretWord)
guess = getGuessLetter(alreadyGuess)
alreadyGuess = alreadyGuess + guess
if guess in secretWord:
correctGuess = correctGuess + guess
else:
errorGuess = errorGuess + guess
if correctGuess == secretWord:
showPlayBroad(errorGuess, correctGuess, secretWord)
print("Congratulations~ Guess Well")
if isPlayAgain():
secretStyle,secretWord = getRandomWord()
alreadyGuess = ''
errorGuess = ''
correctGuess = ''
else:
print("Ok~ See u next time")
break
elif len(alreadyGuess) >= len(HANGMANPICS):
showPlayBroad(errorGuess, correctGuess, secretWord)
print("Oh it is over, the secretWord is {}".format(secretWord))
if isPlayAgain():
secretStyle,secretWord = getRandomWord()
alreadyGuess = ''
errorGuess = ''
correctGuess = ''
else:
print("Ok~ See u next time")
break
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。