Python 达到设定时间后如何停止代码

Python 达到设定时间后如何停止代码,python,timeout,Python,Timeout,我以前问过这个问题,但没有得到有效的回答。我希望玩家完成我的测试,但他们只有一个固定的时间来做,一旦达到规定的时间,游戏结束,并停止问所有的问题 我尝试了多种不同的代码,但下面显示的是我正在尝试的最新代码 import time max_time = int(input('Enter the amount of seconds you want to run this: ')) start_time = time.time() while (time.time() - start_time

我以前问过这个问题,但没有得到有效的回答。我希望玩家完成我的测试,但他们只有一个固定的时间来做,一旦达到规定的时间,游戏结束,并停止问所有的问题

我尝试了多种不同的代码,但下面显示的是我正在尝试的最新代码

import time

max_time = int(input('Enter the amount of seconds you want to run this: '))
start_time = time.time()  
while (time.time() - start_time) > max_time:
    sys.exit()

question_1 = ("Question?")
option_1 =(" a. 54 \n b. 50 \n c. 47 \n d. 38")
print(question_1)
print(option_1)    
answer_1 = input(">")        
if answer_1.lower() == "a":
    print("Correct")  
else:
    print("Incorrect") 

question_a2 = ("Question 2?")
option_a2 = (" a. 4 \n b. 6 \n c. 8 \n d. 10")
print(question_a2)
print(option_a2)            
answer_a2 = input(">")
if answer_a2.lower() == "a":
    print("Correct")
else:
    print("Incorrect")
end_time = time.time()


这段代码只是像平常一样不断地处理问题,什么也没发生。我是一个新手,如果有任何帮助,我将不胜感激。

首先,您应该开始使用函数来最小化代码重复(复制和粘贴)。一个简单但不是真正互动的解决方案是在回答问题后检查时间。替换

if answer_a2.lower() == "a":
    print("Correct")
else:
    print("Incorrect")

在问下一个问题之前,检查
stop\u quick
是否为真,如果为假则继续。我希望你能明白。我还引入了一个变量来计算正确回答的问题

更新:通过使用类存储分数和时间重写测验

import time

class Quiz:
  def __init__(self):
      self.total_points = 0
      self.stop_quiz = False
      self.start_time = time.time()
      self.max_time = int(input('Enter the amount of seconds you want to run this: '))

  def ask_question(self, question, options, correct_answer):
      if self.stop_quiz:
          return
      print(question)
      print(options)
      answer = input(">")
      if (time.time() - self.start_time) > self.max_time:
          print("Sorry, you didn't answer in time. The quiz is over")
          self.stop_quiz = True
      elif answer.lower() == correct_answer:
          print("Correct")
          self.total_points += 1
      else:
          print("Incorrect")

  def get_result(self):
      print("You got {} Points!".format(self.total_points))

quiz = Quiz()
quiz.ask_question("Question 1?", "a. 54 \nb. 50 \nc. 47 \nd. 38", "a")
quiz.ask_question("Question 2?", "a. 54 \nb. 20 \nc. 47 \nd. 38", "b")
quiz.get_result()

使用a我将如何使用它?我该把它放在哪里?你能扩展一下吗?如果你想要100%的动态,比如立即终止测验,那么这是一项非常重要的任务。您需要处理事件和事件。我不建议初学者这样做。但是有一个更简单的解决办法:在每个答案后检查时间是否到了,然后停止测验,不要计算最后一个问题。谢谢你的帮助!实际上我已经有了这个变量lol。我唯一的问题是停止测验不起作用。它会进入下一个问题?这可能是一个范围问题。我使用一个类和一个类方法创建了一个。
import time

class Quiz:
  def __init__(self):
      self.total_points = 0
      self.stop_quiz = False
      self.start_time = time.time()
      self.max_time = int(input('Enter the amount of seconds you want to run this: '))

  def ask_question(self, question, options, correct_answer):
      if self.stop_quiz:
          return
      print(question)
      print(options)
      answer = input(">")
      if (time.time() - self.start_time) > self.max_time:
          print("Sorry, you didn't answer in time. The quiz is over")
          self.stop_quiz = True
      elif answer.lower() == correct_answer:
          print("Correct")
          self.total_points += 1
      else:
          print("Incorrect")

  def get_result(self):
      print("You got {} Points!".format(self.total_points))

quiz = Quiz()
quiz.ask_question("Question 1?", "a. 54 \nb. 50 \nc. 47 \nd. 38", "a")
quiz.ask_question("Question 2?", "a. 54 \nb. 20 \nc. 47 \nd. 38", "b")
quiz.get_result()