用Python写一个程序 3. 中华优秀传统文化知识问答系统 (1)客户端:系统随机从题库抽题(可以是问答题,10题为一组),用户根据题目要求作答,每题回答完毕系统公布正确答案,系统记录正确答题数目及得分数; (2) 服务器端:题库的增删改查及组卷(例如10题为一卷,要求题库至少100道题)) (3)一位同学负责收集素材、一位负责客户端功能,一位负责服务器端功能。
时间: 2024-03-21 16:44:14 浏览: 120
以下是一个简单实现中华优秀传统文化知识问答系统的Python程序,包括客户端和服务器端两部分。
客户端代码:
```python
import random
import requests
class Quiz:
def __init__(self, question, options, answer):
self.question = question
self.options = options
self.answer = answer
class QuizClient:
def __init__(self, server_url):
self.server_url = server_url
self.score = 0
self.correct_count = 0
def start_quiz(self):
quiz_list = self.get_quiz_list()
for i, quiz in enumerate(quiz_list):
print(f'Question {i + 1}:')
print(quiz.question)
for j, option in enumerate(quiz.options):
print(f'{j + 1}. {option}')
answer = input('Your answer: ').strip()
if answer == quiz.answer:
print('Correct!')
self.score += 10
self.correct_count += 1
else:
print(f'Incorrect! The correct answer is {quiz.answer}')
print(f'Your score is {self.score}')
print(f'You answered {self.correct_count} questions correctly out of {len(quiz_list)}')
def get_quiz_list(self):
response = requests.get(self.server_url)
quiz_list = []
for quiz_data in response.json():
quiz = Quiz(quiz_data['question'], quiz_data['options'], quiz_data['answer'])
quiz_list.append(quiz)
random.shuffle(quiz_list)
return quiz_list[:10]
if __name__ == '__main__':
client = QuizClient('http://localhost:5000/quiz')
client.start_quiz()
```
服务器端代码:
```python
from flask import Flask, jsonify, request
import random
app = Flask(__name__)
QUIZ_LIST = [
{
'question': '中国古代四大美书之一是?',
'options': ['《红楼梦》', '《西游记》', '《水浒传》', '《三国演义》'],
'answer': '《红楼梦》'
},
{
'question': '下列哪位是唐代著名诗人?',
'options': ['李白', '苏轼', '辛弃疾', '杜甫'],
'answer': '杜甫'
},
{
'question': '《论语》的作者是?',
'options': ['孔子', '孟子', '荀子', '韩非子'],
'answer': '孔子'
},
# 更多题目...
]
@app.route('/quiz', methods=['GET'])
def get_quiz():
quiz_list = random.sample(QUIZ_LIST, 100)
quiz_data_list = []
for quiz in quiz_list:
quiz_data = {
'question': quiz['question'],
'options': quiz['options'],
'answer': quiz['answer']
}
quiz_data_list.append(quiz_data)
return jsonify(quiz_data_list)
if __name__ == '__main__':
app.run()
```
运行服务器端程序后,在客户端程序中调用服务器端的API接口,可以实现随机抽取题目并进行答题的功能。同时,服务器端也可以方便地进行题库的管理,包括增删改查和组卷等操作。
阅读全文