码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算

时间:2020-12-03 12:18:42      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:mic   imp   else   mamicode   csdn   load   ==   article   pen   

import random

from fractions import Fraction

def newint():
opr = [‘+‘, ‘-‘, ‘ב, ‘÷‘]
fh = random.randint(0, 3)
n1 = random.randint(1, 20)
n2 = random.randint(1, 20)
rjg = 0
if fh == 0:
rjg = n1 + n2
elif fh == 1:
n1, n2 = max(n1, n2), min(n1, n2)
rjg = n1 - n2
elif fh == 2:
rjg = n1 * n2
elif fh == 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)
rjg = int(n1 / n2)
print(n1, opr[fh], n2, ‘= ‘, end=‘‘)
return rjg


def newfra():
opr = [‘+‘, ‘-‘, ‘ב, ‘÷‘]
fh = random.randint(0, 3)
t1 = random.randint(1, 10)
t2 = random.randint(t1, 10)
n1 = Fraction(t1, t2)
t1 = random.randint(1, 10)
t2 = random.randint(t1, 10)
n2 = Fraction(t1, t2)
rjg = 0
if fh == 0:
rjg = n1 + n2
elif fh == 1:
n1, n2 = max(n1, n2), min(n1, n2)
rjg = n1 - n2
elif fh == 2:
rjg = n1 * n2
elif fh == 3:
n1, n2 = max(n1, n2), min(n1, n2)
rjg = n1 / n2
print(n1, opr[fh], n2, ‘= ‘, end=‘‘)
return rjg

def newtest():
opr = [‘+‘, ‘-‘, ‘ב, ‘÷‘]
print(‘输入题库所需要的题目数量‘)
n=int(input())
rjg=[]
m=0
while m<=(n-1):
fh = random.randint(0, 4)
if fh==0:
print(m+1,end=‘、‘)
rjg.append(newfra())
print(‘ ‘)
else:
print(m+1,end=‘、‘)
rjg.append(newint())
print(‘ ‘)
m=m+1
m=0
print(‘答案:‘)
while m<=(n-1):
print(m+1,‘、‘,rjg[m])
m=m+1



print(‘1、四则运算‘)
print(‘2、制作题库‘)
n=int(input())
if n==1:
print(‘input "0000" to Quit‘)
while True:
fh = random.randint(0, 4)
if fh == 0:
rjg = newfra()
jg = input()
if jg == ‘0000‘:
break;
sr = Fraction(jg)
if sr == rjg:
print(‘right‘)
else:
print(‘error. the Tight answer is‘, rjg)
else:
rjg = newint()
jg = input()
if jg == ‘0000‘:
break;
sr = int(jg)
if sr == rjg:
print(‘right‘)
else:
print(‘error. the Tight answer is‘, rjg)
if n==2:
newtest()

技术图片

 

 

参考:https://blog.csdn.net/weixin_30539835/article/details/99789504

 

四则运算

标签:mic   imp   else   mamicode   csdn   load   ==   article   pen   

原文地址:https://www.cnblogs.com/gong-jie/p/14056314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!