Python 单词猜谜游戏:打乱字母顺序,挑战你的词汇量!
以下是 Python 代码实现:
import random
定义单词列表
words = ['apple', 'banana', 'orange', 'watermelon', 'grape', 'pineapple', 'pear', 'kiwi']
从单词列表中随机选择一个单词
word = random.choice(words)
将单词的字母顺序打乱并存储在列表中
word_list = list(word) random.shuffle(word_list)
将乱序后的单词列表转换为字符串并显示给用户
scrambled_word = ''.join(word_list) print('The scrambled word is:', scrambled_word)
提示用户输入猜测结果
guess = input('What is your guess? ')
不断循环,直到用户猜对为止
while guess != word: print('Wrong! Please try again.') guess = input('What is your guess? ')
猜对后输出提示信息
print('Congratulations! You guessed it!')
原文地址: https://www.cveoy.top/t/topic/oics 著作权归作者所有。请勿转载和采集!