繁体   English   中英

有时间限制的数学测验(同时函数)-高级python

[英]math quiz with a time limit (simultaneous functions) - advanced python

所以我想运行两个程序,一个计时器和一个数学问题。 但总是输入似乎正在停止计时器功能或什至根本不运行。 有什么办法可以解决这个问题吗? 我将保持示例简单。

import time

start_time = time.time()
timer=0
correct = answer
answer = input("9 + 9 = ") 
#technically a math question here
#so here until i enter the input prevents computer reading the code
while True:
    timer = time.time() - start_time
    if timer > 3:
#3 seconds is the limit
    print('Wrong!')
quit()

所以回顾一下,我希望玩家在不到 3 秒的时间内回答这个问题。

3秒后游戏将打印错误并退出

如果玩家在三秒内回答,计时器将在触发“错误”并退出之前“终止”或停止

希望您理解,并非常感谢您的帮助

在 Windows 上,您可以使用msvcrt模块的kbhitgetch函数(我稍微现代化了这个代码示例):

import sys
import time
import msvcrt


def read_input(caption, timeout=5):
    start_time = time.time()
    print(caption)
    inpt = ''
    while True:
        if msvcrt.kbhit():  # Check if a key press is waiting.
            # Check which key was pressed and turn it into a unicode string.
            char = msvcrt.getche().decode(encoding='utf-8')
            # If enter was pressed, return the inpt.
            if char in ('\n', '\r'): # enter key
                return inpt
            # If another key was pressed, concatenate with previous chars.
            elif char >= ' ': # Keys greater or equal to space key.
                inpt += char
        # If time is up, return the inpt.
        if time.time()-start_time > timeout:
            print('\nTime is up.')
            return inpt

# and some examples of usage
ans = read_input('Please type a name', timeout=4)
print('The name is {}'.format(ans))
ans = read_input('Please enter a number', timeout=3)
print('The number is {}'.format(ans))

我不确定您在其他操作系统(研究termios 、 tty 、 select )上究竟需要做什么。

另一种可能性是curses模块,它也具有getch 功能,您可以将其设置为nodelay(1) (非阻塞),但对于Windows,您首先必须从Christopher Gohlke 的网站下载curses。

import time
import curses


def main(stdscr):
    curses.noecho()  # Now curses doesn't display the pressed key anymore.
    stdscr.nodelay(1)  # Makes the `getch` method non-blocking.
    stdscr.scrollok(True)  # When bottom of screen is reached scroll the window.
    # We use `addstr` instead of `print`.
    stdscr.addstr('Press "q" to exit...\n')
    # Tuples of question and answer.
    question_list = [('4 + 5 = ', '9'), ('7 - 4 = ', '3')]
    question_index = 0
    # Unpack the first question-answer tuple.
    question, correct_answer = question_list[question_index]
    stdscr.addstr(question)  # Display the question.

    answer = ''  # Here we store the current answer of the user.
    # A set of numbers to check if the user has entered a number.
    # We have to convert the number strings to ordinals, because
    # that's what `getch` returns.
    numbers = {ord(str(n)) for n in range(10)}

    start_time = time.time()  # Start the timer.

    while True:
        timer = time.time() - start_time

        inpt = stdscr.getch()  # Here we get the pressed key.
        if inpt == ord('q'):  # 'q' quits the game.
            break
        if inpt in numbers:
            answer += chr(inpt)
            stdscr.addstr(chr(inpt), curses.A_BOLD)
        if inpt in (ord('\n'), ord('\r')):  # Enter pressed.
            if answer == correct_answer:
                stdscr.addstr('\nCorrect\n', curses.A_BOLD)
            else:
                stdscr.addstr('\nWrong\n', curses.A_BOLD)

        if timer > 3:
            stdscr.addstr('\nToo late. Next question.\n')

        if timer > 3 or inpt in (ord('\n'), ord('\r')):
            # Time is up or enter was pressed; reset and show next question.
            answer = ''
            start_time = time.time()  # Reset the timer.
            question_index += 1
            # Keep question index in the correct range.
            question_index %= len(question_list)
            question, correct_answer = question_list[question_index]
            stdscr.addstr(question)

# We use wrapper to start the program.
# It handles exceptions and resets the terminal after the game.
curses.wrapper(main)

使用time.time() ,它返回纪元时间(即自 1970 年 1 月 1 日 UNIX 时间以来的秒数)。 您可以将其与开始时间进行比较以获取秒数:

start = time.time()
while time.time() - start < 60:
    # stuff

您可以让计时器在任何时候(即使用户正在输入信息)通过信号将您从代码中拉出,但这有点复杂。 一种方法是使用信号库:

import signal
def timeout_handler(signal, frame):
    raise Exception('Time is up!')
signal.signal(signal.SIGALRM, timeout_handler)

这定义了一个引发异常并在超时发生时调用的函数。 现在您可以将 while 循环放入 try catch 块中并设置计时器:

signal.alarm.timeout(60)
try:
    while lives > 0
        # stuff
except:
    # print score

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM