print("Hello world! - python.jsrun.net .")
import json
import random
def load_questions(file_path='e:\chinese.json'):
"""加载题库文件并返回所有题目"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
print(f"错误:找不到题库文件 '{file_path}'")
return None
except json.JSONDecodeError:
print(f"错误:文件 '{file_path}' 不是有效的JSON格式")
return None
except Exception as e:
print(f"未知错误:{e}")
return None
def display_question(question_data):
"""显示单个题目并获取用户答案"""
print("\n" + "-" * 40)
print(f"问题:{question_data['question']}")
if 'options' in question_data:
print("选项:")
for i, option in enumerate(question_data['options'], 1):
print(f"{i}. {option}")
user_answer = input("请输入你的答案:").strip()
return user_answer
def check_answer(user_answer, correct_answer, options=None):
"""验证用户答案是否正确"""
if options and user_answer.isdigit():
try:
user_choice = int(user_answer) - 1
if 0 <= user_choice < len(options):
user_answer_text = options[user_choice]
return user_answer_text == correct_answer
except:
return False
return user_answer == correct_answer
def run_quiz(questions, num_questions=5):
"""运行一个简短的测验"""
if not questions:
print("没有可用的题目")
return
all_questions = []
for q_type, q_list in questions.items():
all_questions.extend([(q_type, q) for q in q_list])
if not all_questions:
print("题库中没有题目")
return
selected = random.sample(all_questions, min(num_questions, len(all_questions)))
correct_count = 0
for q_type, question in selected:
print(f"\n[{q_type.upper()}]")
user_answer = display_question(question)
is_correct = check_answer(
user_answer,
question['answer'],
question.get('options')
)
if is_correct:
print("✅ 回答正确!")
correct_count += 1
else:
print(f"❌ 回答错误,正确答案是:{question['answer']}")
print("\n" + "=" * 40)
print(f"测验完成!共 {num_questions} 题,答对 {correct_count} 题")
print(f"正确率:{correct_count/num_questions:.2%}")
if __name__ == "__main__":
questions = load_questions()
if questions:
for q_type, q_list in questions.items():
print(f"共有 {len(q_list)} 道{q_type}题目")
run_quiz(questions, num_questions=5)