在python中用for循环来实现猜单词的小游戏
1.需求(1)在代码中输入相应的单词,然后将单词打乱,在输入在屏幕上,最后要求输入相应的正确单词(2)在猜单词这项游戏中要求用到for...等循环语句2.代码实现#猜单词游戏import randomWORDS = ("python","apple","barren","create","clearing","estate","hive","shove","design")print("欢迎来到猜
·
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 中的步调(第三参数)
更多推荐
已为社区贡献2条内容
所有评论(0)