随机生成四则运算(小学生要能做)
Posted ganny
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机生成四则运算(小学生要能做)相关的知识,希望对你有一定的参考价值。
要小学生能做的四则运算,要求十以内所以我在前两个运算子程序中,放入了一个while循环,将得到的result进行判断,大于十的式子将不会被输出,而是再次执行程序,直到结果小于10时,输出计算式以及答案
import fractions
from fractions import Fraction
import random
def integer():
d=0
result = 100
while True:
if result>=10:
symbol = [‘+‘, ‘-‘, ‘ב, ‘÷‘]
x = random.randint(0, 3)
n1 = random.randint(1, 10)
n2 = random.randint(1, 10)
if x == 0:
result = n1 + n2
elif x == 1:
n1, n2 = max(n1, n2), min(n1, n2)
result = n1 - n2
elif x == 2:
result = n1 * n2
elif x == 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)
if result>1:
result=100
else:
pass
else:
break
print(n1, symbol[x], n2, ‘= ‘, end=‘‘)
return result
def fraction():
result = 100
d=0
while True:
if result>=10:
symbol = [‘+‘, ‘-‘, ‘ב, ‘÷‘]
x = 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)#化简
if x == 0:
result = n1 + n2
if result>1:
result=100
else:
pass
elif x == 1:
n1, n2 = max(n1, n2), min(n1, n2)
result = n1 - n2
if result>1:
result=100
else:
pass
elif x == 2:
result = n1 * n2
if result>1:
result=100
else:
pass
elif x == 3:
n1, n2 = max(n1, n2), min(n1, n2)
result = n1 / n2
if result>1:
result=100
else:
pass
else:
break
print(n1, symbol[x], n2, ‘= ‘, end=‘‘)
return result
def library():
print(‘输入题库所需要的题目数量‘)
n=int(input())
result=[]
m = 0
while m<=n-1:
x = random.randint(0, 4)
if x==0 :
print(m+1,end=‘ ‘)
result.append(fraction())#将结果放入result库
print(‘ ‘)
else:
print(m+1,end=‘ ‘)
result.append(integer())#将结果放入result库
print(‘ ‘)
m=m+1
m = 0#序数归零
print(‘答案:‘)
while m<=n-1:
print(m+1,‘ ‘,result[m])#在组中序数从0开始,必须m<=n
m=m+1
#begin
print(‘1、四则运算‘)
print(‘begin‘)
library()
input("按任意键退出")
以上是关于随机生成四则运算(小学生要能做)的主要内容,如果未能解决你的问题,请参考以下文章