速算 计应192(西) 第七组 李胜磊
import random
import docx
import re
import time
#产生随机数字
def Number(m,n):
return str(random.randint(m,n))
#产生随机运算符号
def caculate_symbol():
symbol =['+','-','×','÷']
return str(random.choice(symbol))
#生成题目列表
def Test(target_two,target_three):
subject_all =[]
for i in range(0,target_three):
subject = ""
for x in range(0,target_two):
symbol = caculate_symbol()
subject +=(Number(k,j) + symbol)
subject = subject[0:-1] + "="
subject_all.append(subject)
return subject_all
#将答案保存在列表
def Answer(subject_all):
answer = []
for i in subject_all:
a = ""
c = re.sub('×',"*",i)
b = re.sub('÷',"/",c)
question = re.sub("=","",b)
a += i + str("%.1f" %(eval(question)))
answer.append(a)
return answer
#保存为题目
def Save_Test(subject_all):
doc = docx.Document()
doc.add_heading("加减乘除速算(答案保留1位小数)")
doc.add_paragraph('\n')
for i in subject_all:
doc.add_paragraph(i)
doc.save("速算试卷.docx")
print("\033[1;33;1m出题成功!\033[0m")
#保存答案
def Save_answer(answer):
doc = docx.Document()
doc.add_heading("加减乘除速算答案(答案保留一位小数)")
for i in answer:
doc.add_paragraph(i)
doc.save("速算试卷答案.docx")
print("\033[1;34;1m答案已给出!\033[0m","\033[1;31;1m题数为:%s个\033[0m" %len(answer))
#实现主要逻辑
def run():
try:
target_one = int(input("请输入运算数字的位数:"))
global j
j = 1
for i in range(0,target_one):
j *=10
global k
k = j // 10
target_two = int(input("请输入运算数字的个数:"))
target_three = int(input("请输入题数:"))
subject_all = Test(target_two,target_three)
Save_Test(subject_all)
answer = Answer(subject_all)
Save_answer(answer)
except:
print("\033[1;31;1m请按照提示正确输入!\033[0m")
if __name__ == '__main__':
while True:
run()
time.sleep(0.05)
quite = input("\033[1;31;1m按q键退出(任意键继续出题!)\033[0m")
if quite == 'q':
print("\033[1;36;1m已退出程序!\033[0m")
break
else:
print("\033[1;31;1m提示:继续出题会覆盖原来的题目\033[0m")
continue
计应192(西)
第七组
李胜磊