一个随机单词生成器,让用户键入要确认的单词

A random word generator that has the user type the word to confirm

提问人:jmkmac 提问时间:6/8/2023 最后编辑:A-yjmkmac 更新时间:6/8/2023 访问量:124

问:

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

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:')
python 函数 if-statement while-loop

评论

0赞 jmkmac 6/8/2023
我添加了一个嵌套的 while 循环,现在我可以让它确认正确答案,但不会生成另一个单词。我会尝试一些有目的的建议,并回复大家,谢谢

答:

1赞 guidot 6/8/2023 #1

while True开始一个无休止的循环,所以你必须小心,它终止了。

最简单的解决方案是在分支的末尾添加 a。breakthen

评论

0赞 jmkmac 6/8/2023
“然后”分支又在哪里?
0赞 Michael Cao 6/8/2023 #2

您可以添加一个退出词,例如“quit”,并检查该词以终止循环。然后,在得到正确答案时,实现更多的逻辑,无论用户是想继续还是退出。

此外,将 import 语句保留在函数之外。否则,它会在每次函数运行时执行冗余导入。

import random


def word_list():
    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(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

    # Continue looping until user exits by typing "Quit"

    while answer.lower() != "quit":
        if answer == question:
            print("Great job you spelled it correctly!")

            answer = input('Press enter again for a new word or type "quit" to quit')
            
            
            if answer.lower() != "quit":
                question = random.choice(list(words))
                print(question)
                answer = input("Retype the word and press enter:")

        else:  # Has the user re-type the word if it was wrong
            print("You spelled that word wrong, please try again!")
            answer = input("Retype the word and press enter:")
0赞 Lohmar ASHAR 6/8/2023 #3

你走在正确的轨道上,但正如@guidot所说,你必须退出那个循环,你需要第二个循环才能继续播放。
我会把代码写成这样:

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!')

评论

0赞 jmkmac 6/8/2023
正是我想要的,谢谢你的建议!我终于想起了嵌套循环。有没有人知道除了 Visio 之外还有什么免费程序可以帮助我更好地规划代码?再次感谢大家!
1赞 JonSG 6/8/2023 #4

您似乎需要一个使用嵌套循环的解决方案。一个是获取一个单词,然后是一个内部循环,用于从用户那里获得响应,直到他们正确为止。我认为对你的代码进行一个非常小的更改可能会允许你这样做:

import random #Import random function

def word_list(): 
  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')
      return

    print('You spelled that word wrong, please try again!')
    answer = input('Retype the word and press enter:')

while True:
  word_list()

您现在可能会寻找一种方法来简化这一点,但这应该会让您再次开始。

1赞 Abion47 6/8/2023 #5

这里有一个循环的小技巧。你不需要把所有的东西都放在里面,只需要你想重复的东西。

在此示例中,您不想将 main 放在循环中,因为这不是您要重复的内容。您重复的是告诉用户他们弄错了并再次输入的部分。input

所以你要做的是让第一个在循环之外,只把重试放在里面。input

import 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\n"]

def word_list(): 
  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 answer != question:  
    print('You spelled that word wrong, please try again!')
    answer = input('Retype the word and press enter:')

  print('Great job you spelled it correctly!')

你问题的第二部分意味着你想要另一个循环。为了保持函数简单,理想情况下,您希望另一个循环位于您调用的任何位置,而不是位于其本身。最好将每个函数限制在其用途内,而不是试图在一个函数中完成所有操作。word_listword_list

while True:
  word_list()
  
  input('Press enter for another word')
1赞 Matheus Brito 6/8/2023 #6

问题出在 while 循环中。我将代码重新组织成函数,使其更具可读性。

def replay():

   return input('Play again Yes[Y] or N[N]')

def player_answer(question):

   answer = ''
   correct = False

while answer != question:
    answer = input('Retype the word and press enter:')

if answer == question:
        print('Great job you spelled it correctly!')
        correct = True

                
    
return correct
    

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('Sorted word: ' + question)
return question #Displays the word that was generated


  game_on = True
  #lógica do game
  while True:


while game_on:
    
    question = word_list()
    answer = player_answer(question)
    
    if(answer):
        play_again = replay()
        if play_again == 'Y':
            question = word_list()
            answer = player_answer(question)
        else:
            print("Thanks for play!")
            game_on = False

break
    

自行调整