编程语言
首页 > 编程语言> > 自动生成四则运算题目程序(计算机181陈硕)

自动生成四则运算题目程序(计算机181陈硕)

作者:互联网

https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18

https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377

需求分析:
1.能自动生成四则运算题目
2.除了整数外,还要支持真分数的四则运算
3.判断运算结果对错

3180701122

import random
from fractions import Fraction

def newint() 生成整数四则运算

def newint():
opr = ['+', '-', '×', '÷']
#以下fh取符号,n1、n2取随机整数
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() 生成真分数四则运算

def newfra():
opr = ['+', '-', '×', '÷']
#创造n1、n2为随机分数
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() 生成制定指定数量的四则运算题目

def newtest():
opr = ['+', '-', '×', '÷']
print('输入题库所需要的题目数量-n:')
n=int(input())
#建立一个答案列表rig[]
rjg=[]
#m为题序
m=0
while m<=(n-1):
#随机四则运算,设置分数四则运算的概率为1/4,整数为3/4
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 n1:
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('Bingo!')
else:
print('Sorry, the true answer is', rjg)
else:
rjg = newint()
jg = input()
if jg == '0000':
break;
sr = int(jg)
if sr == rjg:
print('Bingo!')
else:
print('Sorry, the true answer is', rjg)
if n
2:
newtest()


psp

总结
1、知识水平有限,对问题理解不够透彻,需提高自身知识素养
2、对Python编程语言不够熟练,今后加强练习
3、对撰写论文还属于初级阶段,今后多写多练

标签:random,四则运算,陈硕,rjg,print,181,fh,n1,n2
来源: https://www.cnblogs.com/TheShuo/p/13916609.html