简体   繁体   English

计时器,日期和脚本。

[英]Timers, dates and script.

So i'm doing a project and have wanted to add some extra features to make it unique. 所以我正在做一个项目,并且想要添加一些额外的功能以使其独特。

How do I make it output a timer sort of thing where it would say "It took you (minutes:seconds) to answer this question" after each question and then a overall timer at the end of the exam (Script). 我如何使它输出一个计时器类的东西,在每个问题之后都会说“花了您(分钟:秒)来回答这个问题”,然后在考试结束时显示了一个整体计时器(脚本)。 Also, how would I add a date on when the exam was taken. 另外,我如何添加考试日期。 What else could i add to make this more unique and users actually interested in doing this. 我还可以添加其他内容以使其变得更加独特,而用户实际上对此很感兴趣。 I have simple input features where it asks for their name, class name and then adds up the total score and outputs into a file, named after the class name using 'with' and 'open' commands. 我有简单的输入功能,其中要求输入名称,班级名称,然后将总分加起来并输出到文件中,并使用“ with”和“ open”命令以班级名称命名。

OPERATIONS = [             # this is stating what the operations are.
    (operator.add, "+"),
    (operator.mul, "*"),
    (operator.sub, "-")
    ]
for _ in range(10):
    num1 = random.randint(1,10)#This will randomly select num1 & num2 to be from 1-10
    num2 = random.randint(1,10)
    op, symbol = random.choice(OPERATIONS) #this will make the symbol equal to the operations and randomly select it by using .choice
    print("What is", num1, symbol, num2,"?")
    if get_int_input() == op(num1, num2):#this will check if the input is true or false by using ValueError if its false.
        print("Well done",name,"you got it correct!")
        score += 1
    else:
        print("Incorrect, better luck next time!")

You can keep state of when you asked the question by using datetime.now() and checking how long it took with datetime.now() - prior time like below. 您可以使用datetime.now()并检查datetime.now()花费了多长时间来保持询问状态,如下所示。 Getting the date is pretty easy as well, datetime.strftime("DD-MM-YYYY") but what I had wrote below has the added benefit of keeping the day as well. datetime.strftime(“ DD-MM-YYYY”)也很容易获得日期,但是我在下面写的内容也具有保留日期的附加好处。

OPERATIONS = [             # this is stating what the operations are.
    (operator.add, "+"),
    (operator.mul, "*"),
    (operator.sub, "-")
    ]
test_begin = datetime.now()
for _ in range(10):
    num1 = random.randint(1,10)#This will randomly select num1 & num2 to be from 1-10
    num2 = random.randint(1,10)
    op, symbol = random.choice(OPERATIONS) #this will make the symbol equal to the operations and randomly select it by using .choice
    start = datetime.now()
    print("What is", num1, symbol, num2,"?")
    if get_int_input() == op(num1, num2):#this will check if the input is true or false by using ValueError if its false.
        print("Well done",name,"you got it correct!")
        score += 1
    else:
        print("Incorrect, better luck next time!")
    time_taken = datetime.now() - start

test_length = datetime.now() - test_begin
print(test_length.strftime("DD-MM-YYYY"))

What you want to do is make use of time from the time module. 你想要做的是利用time时间模块。

You want one timer that starts when your entire application runs, and then you want a question timer that you reset after every question. 您需要一个计时器,该计时器在整个应用程序运行时启动,然后您需要一个在每个问题之后重置的问题计时器。

Here is an example. 这是一个例子。 I leave the refactoring of injecting in to your code up to you. 我将重构注入代码留给您。

Make use of a dictionary to store your your results so you can easily look them up later. 利用字典来存储您的结果,以便以后可以轻松查找它们。 And then you can just output your dictionary for your results. 然后,您可以仅输出字典以获取结果。

from time import time

exam_time = {}

exam_timer_start = time()
for i in range(1, 10):
    question_timer_start = time()
    answer = input('question')
    exam_time["question_{}".format(i)] = time() - question_timer_start
    question_timer_end = time() - question_timer_start
    print("That question took you {}s to complete".format(question_timer_end))
exam_time["total_exam"] = question_timer_end

print(exam_time)

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

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