随机生成四则运算(小学生要能做)

要小学生能做的四则运算,要求十以内所以我在前两个运算子程序中,放入了一个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("按任意键退出")

__EOF__

本文作者Oxygen_bean
本文链接https://www.cnblogs.com/GanNy/p/11922063.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   氧气2019  阅读(161)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示