直接上代碼,解釋在注釋,不懂評論留言,必回
'''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) : ")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/174574.html
標籤:其他
上一篇:域名注冊真的很復雜嗎?
