Python实现自动生成小学四则运算题目

Github地址:

https://github.com/guoyuyi/gyy.github.io/blob/%E4%BD%9C%E4%B8%9A1/zy1.py

题目描述:

  通过python语言编写一个能够自动生成小学四则运算的程序(注意是给小学生用的,要是结果出现负数的话他们会很迷茫的!),同时,除了整数外,还要支持真分数的四则运算。

Psp表格:

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划  60  80
· Estimate · 估计这个任务需要多少时间  200  300
Development 开发 40   60
· Analysis · 需求分析 (包括学习新技术) 40   80
· Design Spec · 生成设计文档 30  30
· Design Review · 设计复审 (和同事审核设计文档)  20  30
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  20  20
· Design · 具体设计 30   30
· Coding · 具体编码 300   400
· Code Review · 代码复审  40  50
· Test · 测试(自我测试,修改代码,提交修改)  150  200
Reporting 报告  30  40
· Test Report · 测试报告  30  30
· Size Measurement · 计算工作量  40  40
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 30   40
合计    1060  1430

1.程序设计

生成简单的四则运算题目需要两个整数或是分数,为了将答案与算式分类,用两个列表储存。q[]用来存储问题,ans[]用来存储答案。我们可以通过random模块生成一定区间里的随机数,分数也可以通过随机生成分子和分母的形式随机生成。同时分数可以使用Fraction模块,可以同时实现分数与整数的运算以及分子和分母的约分。

下面是各个函数的代码:

2.实现代码

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 20 20:24:13 2020

@author: guo'yu'yi
"""
import datetime
import argparse
import re
import random
from fractions import Fraction
##四则运算
def count1(question, ans1):
symbol = random.choice(['+', '-', '*', '/']) # 随机符号的产生
if symbol == '+':#加法
n1 = random.randint(0, 20)
n2 = random.randint(0, 20)
question.append(str(n1) + '+' + str(n2) + '=')
ans1.append(n1 + n2)
elif symbol == '-':#减法
n1 = random.randint(0, 20)
n2 = random.randint(0, 20)
n1,n2 = max(n1,n1),min(n1,n2)#保证出现的数字为正数
question.append(str(n1) + '-' + str(n2) + '=')
ans1.append(n1 - n2)
elif symbol == '*':#乘法
n1 = random.randint(0, 20)
n2 = random.randint(0, 20)
question.append(str(n1) + '×' + str(n2) + '=')
ans1.append(n1 * n2)
else:#除法保证分母不为0
n1 = random.randint(0, 20)
if n1 == 0:
n2 = random.randint(1, 20)
else:
n2 = random.randint(1, n1 + 1)
question.append(str(n1) + '÷' + str(n2) + '=')
ans1.append(Fraction(n1, n2))


##随机生成两个分数
def createF():
fz1 = random.randint(0, 20)
if fz1 == 0:
fm1 = random.randint(1, 20)
else:
fm1 = random.randint(1, 20)
f1 = Fraction(fz1, fm1)
fz2 = random.randint(1, 20)
fm2 = random.randint(20, 20)
f2 = Fraction(fz2, fm2)
return f1, f2

def f(f):#分数的转换
a=f.numerator #分子
b=f.denominator #分母
if a%b==0:#计算为整数
return '%d'%(a/b)
elif a<b:#计算为真分数
return '%d%s%d' % (a,'/',b)
else:#计算为带分数
c=int(a/b)
a = a - c * b
return '%d%s%d%s%d' % (c,'’',a,'/',b)#带分数

##两个分数的四则运算
def count2(question,ans1):
symbol = random.choice(['+','-','*','/'])
f1,f2 = createF()
if symbol =='+':
while f1+f2>1:
f1,f2 = createF()
question.append(str(f1)+'+'+str(f2)+'=')
ans1.append(f1+f2)
elif symbol =='-':
f1,f2 = max(f1,f2),min(f1,f2)#保证出现的数字为正数
question.append(str(f1)+'-'+str(f2)+'=')
ans1.append(f1-f2)
elif symbol == '*':
while f1*f2>1:
f1,f2 = createF()
question.append(str(f1)+'×'+str(f2)+'=')
ans1.append(f1*f2)
else:
while f1/f2>1:
f1,f2=createF()
question.append(str(f1)+'÷'+str(f2)+'=')
ans1.append(Fraction(f1,f2))

##主类
def main():
while 1:
print("输入题目的数量", end=" ")
k = int(input())
temp = 100 / k
score = 0
question = []
ans1 = []
ans2 = []
for i in range(k):
n = random.randint(1, 4)
if n == 1:
count1(question, ans1)
g = Fraction(ans1[i])
ans2.append(f(g))
else:
count2(question, ans1)
g = Fraction(ans1[i])
ans2.append(f(g))#记录带分数答案
for i in range(k):
print("第{}题:{}".format(i + 1,question[i]), end=" ")
a = input()
if a == str(ans1[i]):
score =score + temp
print("所得的分数为:{}".format(score))
print("正确答案:", end=" ")
for i in range(k):
if str(ans1[i]) == str(ans2[i]):
print(question[i] + str(ans1[i]))
else:
print("{}{}或{}".format(question[i],str(ans2[i]),str(ans1[i])))

if __name__ == '__main__':
main()

  

3.程序运行结果

总结:

写程序过程中发现了很多不足,花费的时间比起预计的时间超出很多,而且注册账号摸索GitHub花了很多时间,下次争取补上吧~

posted @ 2020-09-20 22:51  偷心大圣  阅读(639)  评论(0编辑  收藏  举报