带用户单词输入确认的随机单词生成器及Python源码实现
带用户单词输入确认功能的随机单词生成器
这款工具可随机生成单词,要求用户输入该单词以确认拼写是否正确。拼写错误时会持续提示用户重试,拼写正确后可触发生成下一个单词继续练习。
源代码实现
def word_list(): import random # 导入random模块 words = ['hello',"yes","no","I","look","where","can","he","with","see","do","we","a","you","play","am","what","one","the","this","have","on","want","go","like","and","hurt","to","under","day","is","or","of","it","are","said","big","up","that","little","down","there","my","she","out","good","her","all","yes","make","read","no","they","your"] # 从单词列表中随机选取单词 question = random.choice(words) # 输出生成的随机单词 print(question) # 提示用户重新输入单词 answer = input('请重新输入该单词并按回车:') while True: # 循环实现重复练习功能 # 判断用户输入是否正确 if answer == question: print('拼写正确,做得好!') input('按回车键获取下一个单词') # 重新生成新单词 question = random.choice(words) print(question) answer = input('请重新输入该单词并按回车:') else: # 拼写错误时循环提示重试 while answer != question: print('你拼写错了,请再试一次!') answer = input('请重新输入该单词并按回车:')
注:原代码中单词列表末尾的your\n存在多余换行符,已修正为your以避免拼写匹配失败;同时补充了正确拼写后重新生成新单词的逻辑,让工具可持续进行练习。
内容的提问来源于stack exchange,提问作者jmkmac
相关产品推荐
相关产品推荐

