这是100以内的加减法自动生成代码
# -*- coding=utf-8 -*- import sys import random import time sys.setrecursionlimit(100) def randint(): return random.randint(0, 100) def get_add_result(x, y): if x + y <= 100: print('%2d + %2d = ? ' % (x, y)) time.sleep(3) print('答案是:') return ('%2d + %2d = %2d ' % (x, y, x + y)) else: return get_add_result(randint(), randint()) def get_sub_result(x, y): if x - y > 0: print('%2d - %2d = ? ' % (x, y)) time.sleep(3) print('答案是:') return ('%2d - %2d = %2d ' % (x, y, x - y)) else: return get_sub_result(randint(), randint()) def get_calc_type(): calc_type = random.randint(0, 1) if calc_type == 0: return get_add_result(randint(), randint()) else: return get_sub_result(randint(), randint()) def get_result(): for i in range(20): print('请看第%s题:' % (i + 1)) print(get_calc_type()) time.sleep(3) if __name__ == '__main__': get_result()
这个是改进后的100以内的加减混合出题代码
''' 100以内加减法混合出题 Author: Nigel Chen Date: 17/01/2022 ''' import random exp1,exp2 = '','' str1,str2 = '','' j = 0 count = int(input('请输入出题数量:\n')) while j < count: if j < count: flag = random.choice(['+','-','x','÷']) if flag == '+': a = random.randint(0,100) b = random.randint(0,100-a) result = a+b elif flag == '-': a = random.randint(0,99) b = random.randint(0,99) if a < b: a,b = b,a result = a - b elif flag == 'x': a = random.randint(1,99) b = random.randint(1,99) result = a * b else: a = random.randint(1,99) while True: b = random.randint(1,99) if(a % b == 0): break result = int(a / b) a = str(a).ljust(2," ") b = str(b).ljust(2," ") exp1 = a + " " + flag + " " + b + ' =' exp2 = a + " " + flag + " " + b + ' =' + str(result) if j % 2 == 0: str1 = str1 + exp1 + '\t\t' str2 = str2 + exp2 + '\t\t' else: str1 = str1 + exp1 + '\n' str2 = str2 + exp2 + '\n' j = j + 1 ''' with open('math.txt','w') as f: f.write(str1) with open('key.txt','w') as f: f.write(str2) ''' print(count,'道混合加减法题:') print(str1) print(count,'道混合加减法题(带答案):') print(str2)