为什么即使现在满足 while 循环的条件,也会发生递归?

Why does recursion occur even if the while loop's condition is now satisfied?

提问人:Thyme Travler 提问时间:1/22/2023 最后编辑:JoSSteThyme Travler 更新时间:1/22/2023 访问量:70

问:

    questions = ["Python is a coding language \n [1] True \n [2] False", "There are this many coding languages \n [1] 5 \n [2] 3 \n [3] 17 \n [4] Over 500 \n", "question 3", "question 4", "question 5", "question 6", "question 7", "question 8", "question 9", "question 10",]
    
    answers = ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1",]
    
    asked_questions = []
    Number = 0
    import random
    def quiz():
      global questions
      global answers
      global asked_questions
      global Number
      while Number != 10:
        randomnumber = random.randint(0,9)
        random_question = questions[randomnumber]
        asked_questions.append(randomnumber)
        for alreadyaskedquestions in range(len(asked_questions)):
          if randomnumber == asked_questions[alreadyaskedquestions-1] and asked_questions[alreadyaskedquestions] != 0:
            del(asked_questions[-1])
            quiz()
        print(random_question)
        answer = input("")
        Number += 1
        if answer == answers[randomnumber]:
          print("correct")
        else:
          print("wrong")
    
    quiz()

我正在创建一个测验程序,以随机顺序提出 10 个问题。我希望函数在提出 10 个问题后停止,但即使在满足其条件后,while 循环也会继续运行。我尝试添加 print 语句,发现在我的程序中,当提出 10 个问题时,代码不会运行 for 循环之前的行,但我不确定为什么或如何解决这个问题。提前致谢

python while-loop 无限循环

评论

2赞 ricardkelly 1/22/2023
您在 for 循环中的调用似乎不合适。代码中的递归用途是什么?quiz()
0赞 Bohemian 1/22/2023
删除递归调用
0赞 Thyme Travler 1/22/2023
@ricardkelly 它意味着重新启动循环,直到它生成一个与尚未被问到的问题相对应的数字
0赞 Thyme Travler 1/22/2023
@Bohemian如果我不应该在内部调用函数,我应该如何让程序选择一个没有被问到的问题呢?
1赞 Bohemian 1/22/2023
@ThymeTravler通常的方法是将问题放在一个列表中,然后对列表进行洗牌。

答:

0赞 ricardkelly 1/22/2023 #1

这是实现您要做的事情的一种不同方式:

import random

def quiz(questions, answers):
  quiz_remaining = [{'q': questions[i], 'a': answers[i]} for i in range(0,len(questions))]
  question_count = 0
  while question_count < 10:
    question_count += 1
    random_number = random.randint(0,len(quiz_remaining)-1)
    random_quiz = quiz_remaining[random_number]
    del quiz_remaining[random_number]
    print(random_quiz['q'])
    answer = input("")
    if answer == random_quiz['a']:
      print("correct")
    else:
      print("wrong")

questions = ["Python is a coding language \n [1] True \n [2] False", "There are this many coding languages \n [1] 5 \n [2] 3 \n [3] 17 \n [4] Over 500 \n", "question 3", "question 4", "question 5", "question 6", "question 7", "question 8", "question 9", "question 10",]
answers = ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1",]

quiz(questions, answers)

它与你的代码之间存在一些差异:

  • 使用全局变量通常是不可取的。它使您的代码的可移植性降低,并且通常可读性降低。
  • 您无需递归到函数中即可重新启动,因为您已经有了 while 循环。
  • 如果你复制了问题和答案,然后在你提出问题时删除每个问题,那么你可以从剩余的问题中随机选择,而不需要检查你是否已经问过了。
  • 将问题和答案压缩到单个数据结构中,以便您一起处理它们。
1赞 Bohemian 1/22/2023 #2

使用包含问题文本和答案的类。给他们一个列表并随机化顺序:question

import random

random.shuffle(questions)

然后让他们循环播放洗牌列表。