猜单词游戏
思路
-
一个words列表里存放若干的单词,例如:
-
在words列表里随机取出一个单词放进word列表,例如:
-
用一个tips列表存放提示信息,长度跟随机取出来的单词长度相同,而且初始化如下图:
-
一个列表放随机数,长度跟随机取出来的单词长度相同的,且不重复。
-
当用随机数列表里的前两个元素,用word列表里的元素替换提示信息列表的元素:
-
剩下就是游戏逻辑了,需要提示的时候,遍历下一个随机数列表,然后替换信息
直接上代码:
import random
words = ["extends", "private", "static", "public", "void", "return", "super","package","throws"]
word = list(words[random.randint(0, len(words) - 1)])
ranList = random.sample(range(0, len(word)), len(word))
tips = list()
for i in range(len(word)):
tips.append("_")
tips[ranList[0]] = word[ranList[0]]
tips[ranList[1]] = word[ranList[1]]
def showMenu():
print("需要提示请输入'help?'")
print("结束游戏请输入'quit!'")
def showtips():
for i in tips:
print(i, end=" ")
print()
def needTips(tipsSize):
if tipsSize <= len(word)-3:
tips[ranList[tipsSize]] = word[ranList[tipsSize]]
tipsSize += 1
return tipsSize
else:
print("已没有提示!")
def init():
print("------java关键字版本-------")
tipsSize = 2
showMenu()
while True:
print("提示:",end="")
showtips()
guessWord =
input("猜一下这个单词:")
if guessWord == ''.join(word):
print("恭喜你,猜对了!就是%s!"%(''.join(word)))
break
elif guessWord == 'help?':
tipsSize = needTips(tipsSize)
elif guessWord == 'quit!':
break
else:
print("猜错了!")
continue
init()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
运行结果:
点个赞支持一下叭!谢谢!