计时器,日期和脚本。

时间:2016-02-25 22:01:48

标签: python

所以,我正在做一个项目,并希望添加一些额外的功能,使其独一无二。

如何让它输出一个定时器类型的东西,它会说"花了你(分钟:秒)回答这个问题"在每个问题之后,然后在考试结束时的整个计时器(脚本)。另外,我如何在考试时添加日期。我还能添加什么来使其更加独特,用户实际上对此感兴趣。我有简单的输入功能,它会询问他们的姓名,班级名称,然后将总分和输出加到一个文件中,以类名命名,使用''并且'打开'命令。

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

2 个答案:

答案 0 :(得分:0)

您可以通过使用datetime.now()并查看datetime.now()所花费的时间来保持您提问时的状态 - 如下所示。获取日期也非常简单,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"))

答案 1 :(得分:0)

您要做的是使用time模块中的time

您希望在整个应用程序运行时启动一个计时器,然后您需要在每个问题后重置的问题计时器。

这是一个例子。我将重构注入到您的代码中。

使用字典存储您的结果,以便日后查看。然后您可以输出您的字典以获得结果。

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)