except DuplicateError: # print("题目重复,删除该式子") continue else: num += 1 return self.formula, self.answer @staticmethod def duplicate_check(target_expr, result_expr): """检查新生成的式子是否重复""" for expr in result_expr: if expr == target_expr: return True if target_expr[0] == '+' or target_expr[0] == '*': temp = target_expr[1] target_expr[1] = target_expr[2] target_expr[2] = temp if expr == target_expr: return True return False return False if __name__ == '__main__': t = Tree() e_file = os.path.join(os.getcwd(), 'Exercises.txt') a_file = os.path.join(os.getcwd(), 'Answer.txt') g_file = os.path.join(os.getcwd(), 'Grade.txt') formula_list, ans_list = t.generate_formula(10, 100, False) FileUtils.write_file(formula_list, ans_list, e_file, a_file) # 保存题目文件
args = parser.parse_args() if __name__ == '__main__': if args.range is None: print("请输入'-r'参数控制题目中数值(自然数、真分数和真分数分母)的范围") if args.exercise is None: args.exercise = os.path.join(os.getcwd(), 'Exercises.txt') if args.answer is None: args.answer = os.path.join(os.getcwd(), 'Answer.txt') if args.grade is None: args.grade = os.path.join(os.getcwd(), 'Grade.txt') print("欢迎进入答题模式......(输入'exit'可退出程序)") t = Tree() u_answer = list() # 用户答案 formula, s_answer = t.generate_formula(args.range, args.number, args.minus) # 随机生成表达式 FileUtils.write_file(formula, s_answer, args.exercise, args.answer) # 保存题目文件 for i in range(args.number): print(formula[i], end='') answer = input() # 获取用户输入的答案 if answer == 'exit': print('退出程序成功!') sys.exit() u_answer.append(answer) correct, wrong = CalculatorUtils.grading(u_answer, s_answer) # 统计答题结果 print("答题结果:") print(correct) print(wrong) FileUtils.write_grade_file(args.grade, correct, wrong) # 保存答题结果