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) : ")
        

猜你喜欢

转载自blog.csdn.net/weixin_43402353/article/details/109089093