微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

100以内加减法混合出题

'''

100以内加减法混合出题
Author: 
Date: 
'''

import random
exp1,exp2 = '',''
str1,str2 = '',''
start = 5
j = 1

# count = int(input('Input question number:\n'))
count = 84 #63
while j < count:
    if j < count:
        flag = random.choice(['+','-'])
        if flag == '+':
            a = random.randint(start,100)
            if 100-a >=start:#random.randint()方法的第二个参数必须大于或等于第1个参数。
                b = random.randint(start,100-a)
                result = a+b
        elif flag == '-':
            a = random.randint(start,99)
            b = random.randint(start,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 = str(j) + '.' + "  "+ a + " " + flag + " " + b + ' ='
        exp2 = str(j) + '.' + "  "+ a + " " + flag + " " + b + ' =' + str(result)
        if j % 4 == 0:
            str1 = str1 + exp1 + '\n\n\n\n\n\n\n\n\n'
            str2 = str2 + exp2 + '\n'
            #print("-----j------",j)
        else:
            str1 = str1 + exp1 + '\t\t'
            str2 = str2 + exp2 + '\t\t'
        j = j + 1
with open('math.doc','w') as f:
    f.write(str1)
with open('key.txt','w') as f:
    f.write(str2)

print(count,'No Answer')
print(str1)
print(count,'With Answer:')
print(str2)

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐