我需要编写一个程序来询问简单的随机添加问题,并找出用户解决此问题所需的时间。该程序应继续询问数学问题,直到被告知停止。它应该找到正确答案的平均值,最快答案和平均答案时间。 我开始编写程序,但它不起作用,所以我甚至不知道如何继续。我无法打印出数学问题。你能否告诉我我做错了什么,并帮助我找到最快的问题和每个问题的平均时间以及平均有多少问题是正确的? 这是运行时问题的样子:
>>> Math()
Are you ready? no
Okay fine
>>> Math()
Are you ready? yes
0+5=5
4 + 9 = 13
3 + 2 = 89 1+5=6
3+3=9
1+7=8
7 + 7 = 14 5+4=9
1 + 8 = stop
You answered 75% problems correctly
Your average answer time is 1.4388 seconds
Your fastest answer time is 1.2247 seconds
这就是我所拥有的:
def fastMath():
ready=input('Are you ready?')
num1 = int(random.randint(0,10))
num2 = int(random.randint(0,10))
operator = '+'
math = ((num1)+ operator +(num2))
if ready == 'no':
print ('Okay fine')
else:
start=time.clock
print (math)
while math = true:
print (math)
答案 0 :(得分:2)
这显然是家庭作业,所以我不会提供完整的代码,只是给出一些关于你做错了什么的指示。
import time
clock
是一个函数,因此您必须调用它,例如clock()
而不是clock
time.time()
比time.clock()
num1 = ...
的部分移动到math = ...
到函数中,将等式返回为字符串并正确的结果while math = true:
没有任何意义。 math
是一个字符串。您必须获取用户input
并将其与预期结果进行比较total_questions
和correct_answers
,然后使用它们计算百分比min
获取最快时间,使用sum
和len
获得平均值。答案 1 :(得分:0)
但是,我绝对不确定一切是否符合预期:
#!/usr/bin/env python3
# coding: utf-8
import time
from random import randint
class MathQuestions:
def __init__(self):
self.start_time = None
self.question_status = list()
self.question_durations = list()
self.ask_num_questions()
def ask_num_questions(self):
'''Ask user how many questions should be solved'''
num_questions = input('How many questions should I ask? ')
try:
num_questions = int(num_questions)
self.handle_questions(num_questions)
except ValueError:
print('This is not a valid number.')
self.ask_num_questions()
def ask_user_ready(self):
'''Ask user if he is ready or not'''
user_ready = input('Are you ready? (yes / [no]) ')
if user_ready != 'yes':
sys.exit(0)
else:
return(time.time())
def handle_questions(self, num):
'''Generate as many questions as wanted and get start time'''
self.start_time = time.time()
for n in range(num):
self.ask_question()
self.stop_questions(self.start_time)
def ask_question(self):
'''Ask a question by gernerating two random integers'''
operators = ['+', '-', '*', '/']
op = operators[randint(0, len(operators)-1)]
num1 = randint(0, 100)
if op == '/':
num2 = randint(1, 100)
else:
num2 = randint(0, 10)
start_time = time.time()
answer = input('{} {} {} = '.format(num1, op, num2))
self.question_durations.append(time.time() - start_time)
if op == '+':
question = num1 + num2
elif op == '-':
question = num1 - num2
elif op == '*':
question = num1 * num2
elif op == '/':
question = num1 / num2
try:
answer = float(answer)
if answer == question:
status = True
else:
status = False
except ValueError:
status = False
self.question_status.append(status)
def stop_questions(self, start_time):
'''Stop questioning and do some user statistics'''
stop_time = time.time() - self.start_time
avg_duration = sum(self.question_durations) / len(self.question_durations)
min_duration = min(self.question_durations)
max_duration = max(self.question_durations)
percentage_correct = (self.question_status.count(1) / len(self.question_status)) * 100
print('Your average duration per answer is: {:3.3f} seconds'.format(avg_duration))
print('Your minimum duration was: {:3.3f} seconds'.format(min_duration))
print('Your maximum duration was: {:3.3f} seconds'.format(max_duration))
print('You answered {:3.3f} percent of all questions correct.\n'.format(percentage_correct))
if __name__ == '__main__':
q = MathQuestions()