python随机出5个两整数相加题,统计出答题正确的时间
时间: 2023-11-27 11:51:40 浏览: 67
下面是一个示例代码,你可以根据你的需要进行修改:
```python
import random
import time
def generate_questions(num):
questions = []
for i in range(num):
a = random.randint(1, 100)
b = random.randint(1, 100)
questions.append((a, b))
return questions
def ask_questions(questions):
correct_count = 0
start_time = time.time()
for a, b in questions:
answer = input(f"What is {a} + {b}? ")
if int(answer) == a + b:
correct_count += 1
end_time = time.time()
return correct_count, end_time - start_time
questions = generate_questions(5)
correct_count, time_taken = ask_questions(questions)
print(f"You got {correct_count} out of {len(questions)} questions correct.")
print(f"Total time taken: {time_taken:.2f} seconds")
```
这个代码会生成 5 个两整数相加题,然后让用户回答这些问题,并统计答题正确的数量和总时间。
阅读全文