让用户键入要确认的单词的随机单词生成器的源代码

def word_list(): 
  import random #Import random function
  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\n"]
  question = random.choice(list(words)) #Generates a random word from the list
  print(question) #Displays the word that was generated
  answer = input('Retype the word and press enter:') #User retypes the 
  while True: #Loop that allows user to 
    if answer == question: #If statement that confirms the user spelled the word right
      print('Great job you spelled it correctly!')
      input('Press enter for another word')
    else: 
      while answer != question:
        print('You spelled that word wrong, please try again!')
        answer = input('Retype the word and press enter:')

推荐答案

You were on the right track, but as @guidot was saying you have to exit that loop, and you need a second loop to keep playing.
I would've written the code something like this:

def word_list(): 
    import random #Import random function
    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"]
    play = True
    # a first loop for the game, as it seemed that you wanted it "infinte"
    while play:
        # here starts a round, get a word
        question = random.choice(words) #Generates a random word from the list
        print(question) #Displays the word that was generated
        while True: # loop until the player types the word correctly
            answer = input('Retype the word and press enter: ') #User retypes the 
            if answer == question:
                # the player has answered correctly, award him
                print('Great job you spelled it correctly!')
                # check if the player wants to keep playing
                answer = input('Press enter for another word, write "exit" to stop: ')
                # if the answer was not exit, keep playing 
                play = answer.strip() != "exit"
                # this exit this loop, so it will end the round
                break
            else:
                # the answer was wrong 
                print('You spelled that word wrong, please try again!')

Python相关问答推荐

Pandas数据帧处理Pandas表中Json内的嵌套列表以获取后续Numpy数组

当测试字符串100%包含查询字符串时,为什么t fuzzywuzzy s Process.extractBests不给出100%分数?

Python中的Pool.starmap异常处理

在Transformer中使用LabelEncoding的ML模型管道

在编写要Excel的数据透视框架时修复标题行

Pydantic:如何将对象列表表示为dict(将列表序列化为dict)

Google Drive API获取文件计量数据

如何根据情况丢弃大Pandas 的前n行,使大Pandas 的其余部分完好无损

计算所有前面行(当前行)中列的值

如何将ctyles.POINTER(ctyles.c_float)转换为int?

难以在Manim中正确定位对象

Python中绕y轴曲线的旋转

PyQt5,如何使每个对象的 colored颜色 不同?'

如何并行化/加速并行numba代码?

让函数调用方程

如何在FastAPI中为我上传的json文件提供索引ID?

为什么if2/if3会提供两种不同的输出?

寻找Regex模式返回与我当前函数类似的结果

如何创建引用列表并分配值的Systemrame列

比Pandas 更好的 Select