Python 单词猜谜游戏:随机打乱字母顺序
以下是 Python 实现:
import random
' 定义单词列表,可以根据实际需求更改 ' words = ['apple', 'banana', 'orange', 'pear', '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.lower() != word: print('Incorrect, try again!') guess = input('Guess the word: ')
' 猜测正确,输出提示信息 ' print('Congratulations, you guessed the word!')
原文地址: https://www.cveoy.top/t/topic/oic3 著作权归作者所有。请勿转载和采集!