Python猜单词小游戏(简约版)

猜单词游戏
思路

  1. 一个words列表里存放若干的单词,例如:["extends", "private", "static", "public"]

  2. 在words列表里随机取出一个单词放进word列表,例如:在这里插入图片描述

  3. 用一个tips列表存放提示信息,长度跟随机取出来的单词长度相同,而且初始化如下图:在这里插入图片描述

  4. 一个列表放随机数,长度跟随机取出来的单词长度相同的,且不重复。在这里插入图片描述

  5. 当用随机数列表里的前两个元素,用word列表里的元素替换提示信息列表的元素:在这里插入图片描述

  6. 剩下就是游戏逻辑了,需要提示的时候,遍历下一个随机数列表,然后替换信息

直接上代码:

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("猜一下这个单词:")
		#  <''.join(word)>把word列表的内容转换成字符串
		if guessWord == ''.join(word):
			print("恭喜你,猜对了!就是%s!"%(''.join(word)))
			break
		elif guessWord == 'help?':
			tipsSize = needTips(tipsSize)
		elif guessWord == 'quit!':
			break
		else:
			print("猜错了!")
			continue
init()

运行结果:在这里插入图片描述在这里插入图片描述
点个赞支持一下叭!谢谢!

猜你喜欢

转载自blog.csdn.net/weixin_44864260/article/details/109298449