Python 单词猜谜游戏:随机字母顺序
以下是 Python 代码实现:
import random
# 定义单词列表
words = ['apple', 'banana', 'orange', 'grape', 'watermelon']
# 随机选择一个单词
word = random.choice(words)
# 打乱单词的字母顺序
letters = list(word)
random.shuffle(letters)
jumbled = ''.join(letters)
# 显示乱序后的单词并提示用户输入猜测结果
print('The jumbled word is:', jumbled)
guess = input('Guess the word: ')
# 判断猜测结果是否正确
while guess != word:
print('Wrong guess. Try again.')
guess = input('Guess the word: ')
print('Congratulations! You guessed the word correctly.')
示例输出:
The jumbled word is: pplea
Guess the word: apple
Congratulations! You guessed the word correctly.
原文地址: https://www.cveoy.top/t/topic/oicV 著作权归作者所有。请勿转载和采集!