猜单词游戏就是计算机随机产生一个单词,打乱字母顺序,供玩家去猜。猜单词游戏的python实现如下:
import random
words = ('python','jumble','difficult','iphone','excellent','outstanding','outside','elegent')
print(
"""
欢迎参加猜单词游戏
把字母组合成一个正确的单词
"""
)
iscontinue = "y"
while iscontinue=="y" or iscontinue=="Y":
word = random.choice(words)
correct = word
outof_order = ""
while word:
position = random.randrange(len(word))
outof_order += word[position]
word = word[:position]+word[(position+1):]
print("乱序后的单词:",outof_order)
guess = input("请猜这个单词是什么:")
while guess != correct and guess!="":
print("啊哦,好像不太对呢,再猜一次吧。")
guess = input("继续猜:")
if guess == correct:
print('猜对了,你太棒了!')
iscontinue = input("还要再来一局吗?(Y/N)")
'''
result:
欢迎参加猜单词游戏
把字母组合成一个正确的单词
乱序后的单词: hponty
请猜这个单词是什么:ihon
啊哦,好像不太对呢,再猜一次吧。
继续猜:python
猜对了,你太棒了!
还要再来一局吗?(Y/N)n
'''