python运算法则_四则运算(Python)

sym = ['+', '-', '×', '÷']

f= random.randint(0, 3)

n1 = random.randint(1, 20)

n2 = random.randint(1, 20)

result = 0

if f== 0:#加法

result = n1 + n2

elif f == 1:#减法,要先比较大小,防止输出负数

n1, n2 = max(n1, n2), min(n1, n2)

result = n1 - n2

elif f== 2:#乘法

result = n1 * n2

elif f == 3:#除法,要比较大小,并循环取整除

n1, n2 = max(n1, n2), min(n1, n2)

while n1 % n2 != 0:

n1 = random.randint(1, 10)

n2 = random.randint(1, 10)

n1, n2 = max(n1, n2), min(n1, n2)

result = int(n1 / n2)

print(n1, sym[f], n2, '= ', end='')

return result

制作题库

def test():

sym = ['+', '-', '×', '÷']

print('输入所需要的题目数量')

n=int(input())

result =[]

m=0

while m<=(n-1):

print(m+1,end='、')

result .append(szys())

print(' ')

m=m+1

m=0

print('对应的答案:')

while m<=(n-1):

print(m+1,'、',result [m])

m=m+1

print('选择想要的模式')

print('1、进行四则运算')

print('2、制作题库')

n=int(input())

当输入1时,进行四则运算,调用函数syzs()

if n==1:

while True:

result = szys()

j= input()

s= int(j)

if s== result :

print('right')

else:

print('error.,the answer is', result )

当输入2时,进行制作题库

if n==2:

test()![](https://img2020.cnblogs.com/blog/2176726/202010/2176726-20201027234025827-906802082.png)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值