Word Jumble 猜單詞遊戲
import random #建立單詞序列
WORDS = ("python", "word","easy","happy","diffcult","play","again")
# start the game
print(""" 猜單詞遊戲 請把字母組合成一個正確單詞 """)
iscontinue="y"
while iscontinue=='y'or iscontinue=='Y':
word = random.choice(WORDS) #從序列中隨機挑選一個單詞
correct = word #一個用於判斷玩家是否猜對的變量
jumble ="" #建立亂序後的單詞
while word:
position = random.randrange(len(word)) #根據word長度,產生word隨機位置
jumble += word\[position\] #將position位置子母組合到亂序後單詞
word = word\[:position\] + word\[(position + 1):\] #經過切片,將position位置字母從原單詞表中刪除
print("亂序後單詞:", jumble)
guess = input("\\n請你寫出你的答案:")
while guess != correct and guess !="":
print("對不起,回答錯誤")
guess = input("請繼續回答:")
if guess == correct:
print("恭喜你,回答正確\\n")
iscontinue=input("\\n\\n是否繼續(Y/N):")