栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

在python中用for循环来实现猜单词的小游戏

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在python中用for循环来实现猜单词的小游戏

1.需求

(1)在代码中输入相应的单词,然后将单词打乱,在输入在屏幕上,最后要求输入相应的正确单词

(2)在猜单词这项游戏中要求用到for...等循环语句

2.代码实现
#猜单词游戏
import random
WORDS = ("python","apple","barren","create","clearing","estate","hive","shove","design")
print("欢迎来到猜单词小游戏 ,请将乱序后的单词组成正确的单词")
is_continue = 'y'
while is_continue == 'y' or is_continue == 'Y' or is_continue == 'YES' or is_continue == 'yes':
	words = random.choice(WORDS)
	right = words
	print(words)
	#打乱字母书序(用切片的方法来实现)
	new_words = ''
	while words:
		position = random.randrange(len(words))
		new_words += words[position]
		words = words[:position] + words[position+1:]
	print("乱序后的单词为:",new_words)
	guess = input("请输入您猜想的单词:")
	while guess != right and guess != '':
		print("您猜错了!请您重新猜一下")
		guess = input("请您继续猜:")
	if guess == right:
		print("您猜对了!")
		is_continue = input("是否继续?请输入yes / no :")
3.结果

 4.注意要求

(1)random 模块

random.choice(words) 选中单词后将选中的单词字母进行打乱顺序 (2)切片模块 可能拼接时候会使用切片 [ 参数 1: 参数 2:step] 中的 step  就是切片的步调,很类似于 for 循环中的 range 中的步调(第三参数)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857831.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号