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

huangapple go评论61阅读模式
英文:

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

问题

以下是用于随机单词生成器的源代码,要求用户输入单词以确认:

def word_list():
  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']
  question = random.choice(list(words)) # 从列表中随机生成一个单词
  print(question) # 显示生成的单词
  answer = input('重新输入单词然后按回车键:') # 用户重新输入单词
  while True: # 循环允许用户
    if answer == question: # 如果语句确认用户拼写正确
      print('做得好,你拼对了!')
      input('按回车键获取另一个单词')
    else: 
      while answer != question:
        print('你拼写那个单词错了,请重试!')
        answer = input('重新输入单词然后按回车键:')
英文:

Source code for a random word generator that has the user type the word to confirm

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

答案1

得分: 1

while True 开始了一个无限循环,所以你必须注意确保它终止。

最简单的解决办法是在 then 分支的末尾加上 break

英文:

while True starts an endless loop, so you have to take care, that it terminates.

The easiest solution is adding a break at the end of the then branch.

答案2

得分: 1

这是您提供的代码的中文翻译:

您似乎需要一个使用嵌套循环的解决方案一个用于获取一个单词然后一个内部循环用于从用户获取响应直到他们答对为止我认为对您的代码进行非常轻微的更改可能会让您实现这一点

```python
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))  # 从列表中随机选择一个单词
    print(question)  # 显示生成的单词

    answer = input('重新输入单词并按回车键:')  # 用户重新输入单词
    while True:  # 允许用户循环输入
        if answer == question:  # 如果语句确认用户拼写正确
            print('干得好,你拼写得对!')
            input('按回车键获取另一个单词')
            return

        print('你拼写错了,请再试一次!')
        answer = input('重新输入单词并按回车键:')

while True:
    word_list()

您现在可以寻找一种简化这个代码的方法,但这应该能让您再次开始。

英文:

You seem to need a solution that uses nested loops. One to get a word then an inner loop to get responses from the user until they get it right. I think a very light change to your code might allow you to do this:

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()

You might now look for a way to simplify this but this should get you going again.

答案3

得分: 1

以下是翻译好的部分:

这里有一个关于循环的小技巧。你不需要把所有东西都放在循环中,只需要重复执行的部分。

在这个示例中,你不希望把主要的 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']

def word_list(): 
  question = random.choice(list(words)) # 从列表中随机选择一个单词

  print(question) # 显示生成的单词
  answer = input('重新输入单词并按回车键:') # 用户重新输入单词

  while answer != question:  
    print('你拼错了这个单词,请再试一次!')
    answer = input('重新输入单词并按回车键:')

  print('干得好,你拼写正确了!')

你问题的第二部分意味着你想要另一个循环。为了保持函数简单,理想情况下,你应该把这个其他循环放在调用 word_list 的地方,而不是在 word_list 本身。最好的做法是将每个函数限制在其目的内,而不是尝试在一个函数中完成所有事情。

while True:
  word_list()
  
  input('按回车键获取另一个单词')
英文:

Here's a little trick with loops. You don't need to have everything in them, only the stuff you want to repeat.

In this example, you don't want to put the main input in the loop, because that's not what you're repeat. What you are repeating is the part where you tell the user that they got it wrong and to input it again.

So what you want to do is to have the first input outside of the loop, and only put the retries inside.

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

The second part of your question means that you want another loop. In the interest of keeping the function simple, ideally you'd want this other loop to be wherever you called word_list rather than in word_list itself. It's good practice to keep each function limited to its purpose instead of trying to do everything in one function.

while True:
  word_list()
  
  input('Press enter for another word')

答案4

得分: 1

Sure, here are the translated parts of your code:

问题出在while循环中我重新组织了代码成函数以使其更易读

def replay():
    
   return input('再玩一次是Yes[Y]还是No[N]')
    
def player_answer(question):
    
   answer = ''
   correct = False
    
   while answer != question:
       answer = input('重新输入单词并按回车键:')
    
   if answer == question:
       print('做得好,你拼写正确!')
       correct = True
  
   return correct

def word_list():
    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']
    question = random.choice(list(words)) #从列表中随机生成一个单词
    print('排序后的单词:' + question)
    return question #显示生成的单词

game_on = True
#游戏逻辑
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("谢谢参与!")
                game_on = False
    
    break

Please note that I've translated the code comments and string literals into Chinese as well.

英文:

the problem is in the while loop. I reorganized the code into functions to make it more readable.

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

make ur own adjustments

答案5

得分: 0

你可以添加一个退出词,如“quit”,并检查是否输入它来终止循环。然后,在用户回答正确时,实现一些逻辑来确定用户是否想继续还是退出。

此外,保持导入语句在函数外部。否则,它会在每次函数运行时进行冗余导入。

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))  # 从列表中生成一个随机单词
    print(question)  # 显示生成的单词
    answer = input("重新输入单词并按回车键:")  # 用户重新输入单词

    # 持续循环,直到用户输入“quit”

    while answer.lower() != "quit":
        if answer == question:
            print("很好,你拼对了!")

            answer = input('再次按回车获取新单词或输入 "quit" 退出')

            if answer.lower() != "quit":
                question = random.choice(list(words))
                print(question)
                answer = input("重新输入单词并按回车键:")
        else:  # 如果用户拼写错误,让用户重新输入单词
            print("你拼错了,请重试!")
            answer = input("重新输入单词并按回车键:")
英文:

You can add an exit word like "quit" and check for that to terminate the loop. Then, implement some more logic whether user wants to continue or quit when they get the right answer.

Also, keep the import statement outside of the function. Otherwise, it does a redundant import every time the function runs.

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:")

答案6

得分: 0

你的代码方向是正确的,但正如 @guidot 所说,你需要退出那个循环,并且需要一个第二个循环来继续游戏。
我会像这样编写代码:

def word_list():
    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']
    play = True
    # 为游戏设置第一个循环,似乎你想让它“无限”进行下去
    while play:
        # 从列表中随机选择一个单词开始一轮游戏
        question = random.choice(words)  # 从列表中随机生成一个单词
        print(question)  # 显示生成的单词
        while True:  # 循环直到玩家正确输入单词
            answer = input('重新输入单词然后按回车键:')  # 用户重新输入单词
            if answer == question:
                # 玩家已经正确回答,奖励他
                print('太棒了,你拼写正确了!')
                # 检查玩家是否想继续游戏
                answer = input('按回车键获取另一个单词,输入“exit”停止:')
                # 如果答案不是“exit”,则继续游戏
                play = answer.strip() != "exit"
                # 这将退出此循环,结束本轮游戏
                break
            else:
                # 答案错误
                print('你拼错了那个单词,请再试一次!')
英文:

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

huangapple
  • 本文由 发表于 2023年6月8日 22:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定