在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 中的步调(第三参数)
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小捣蛋.

建议你还是留着你的小钱吧!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值