pyhton实现猜单词游戏

直接上代码,解释在注释,不懂评论留言,必回

'''1 猜单词游戏
计算机随机产生一个单词,打乱字母顺序,供玩家猜测,采用控制字符界面。例如:
乱序后单词: luebjm
请你猜:jumble
真棒 ,你猜对了!
是否继续(Y/N):
…'''
import re
import random
#随意截取的段落
ch = ''' I like to be happy
      Life is a colorful picture full of different feelings I'd like to be happy because happiness is important to everyone.
      I have an unforgettable experience to share with you.
      Last Sunday my parents gave me some pocket money and with the money I bought some books instead of snacks.
      The next day I took the books to school. After lunch, I showed the books to my classmates and we read together.
      We learned a lot from the interesting books. Both reading and sharing made  me happy,
     How I wish I were happy every day in rnv life!'''
#正则表达式匹配去掉【】内的字符
#re.sub的用法
#split()返回的是一个list
#set转为集合去重
word =re.sub("[\n.,!']" ,"",ch)
words = list(set(word.split()))


print("游戏开始")

isgo = 'y'

while(isgo=='y' or isgo=='Y'):
    rword = random.choice(words)
    cword = rword
    jword = ""
    while rword:
        p = random.randrange(len(rword))
        jword+=rword[p]
        rword = rword[:p]+rword[(p+1):]
    print("乱序后的单词:",jword)
    print()
    gword = input("请输入单词:")
    while gword!=cword and gword!="":
        print("猜错了,请继续")
        gword = input("请再次输入单词")
    if gword == cword:
        print("真棒,你猜对了\n")
    isgo = input("\n请问是否继续(Y/N) : ")
        

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
当然可以!Python单词游戏是一个经典的编程练习项目,通常用于教学或娱乐。它基于随机选择一个单词,玩家需要测这个单词的所有字母。游戏流程一般包括以下几个步骤: 1. **字典选择**:从一个预先定义的单词列表中随机选取一个单词作为目标。 2. **隐藏单词**:用星号(*)表示目标单词中的每个字母,未中的字母隐藏起来。 3. **用户输入**:玩家输入他们测的字母,程序检查这个字母是否在目标单词中。 4. **更新隐藏单词**:如果玩家对了,将正确的字母显示出来;如果错了,提示字母是否存在于单词中。 5. **循环直到对**:当玩家出所有字母后,游戏结束并显示结果。 要实现这个游戏,你可以使用Python的内置模块如random和string,以及一些基本的控制结构(如循环和条件语句)。 下面是一个简单的Python单词游戏的概述代码框架: ```python import random # 准备单词列表 word_list = ["apple", "banana", "cherry", ...] # 选择随机单词 target_word = random.choice(word_list) hidden_word = '*' * len(target_word) # 游戏循环 while True: guess = input("Guess a letter: ").lower() if guess in target_word: hidden_word = hidden_word.replace('*', guess, 1) # 替换正确位置的 * else: print(f"{guess} is not in the word.") # 检查是否出全部单词 if '*' not in hidden_word: print(f"Congratulations! You guessed the word: {hidden_word}") break ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Blockchain_KT

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值