• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
游浩贤
博客园    首页    新随笔    联系   管理    订阅  订阅

小学四则运算

 

 

 

代码如下:

import random
#四则运算

def t():
    sym = ['+', '-', '×', '÷']
    f= random.randint(0, 3)
    n1 = random.randint(1, 20)
    n2 = random.randint(1, 20)
    result = 0
    if f== 0:#加法
        result  = n1 + n2
    elif f == 1:#减法,要先比较大小,防止输出负数
        n1, n2 = max(n1, n2), min(n1, n2)
        result  = n1 - n2
    elif f== 2:#乘法
        result  = n1 * n2
    elif f == 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)
    print(n1, sym[f], n2, '= ', end='')
    return result
#制作题库
def test():

    print('输入所需要的题目数量')
    n=int(input())
    result =[]
    m=0
    while m<=(n-1):
        print(m+1,end='、')
        result .append(t())
        print(' ')
        m=m+1
    m=0
    print('对应的答案:')
    while m<=(n-1):
        print(m+1,'、',result [m])
        m=m+1
print('选择想要的模式')
print('1、进行四则运算')
print('2、制作题库')
n=int(input())
#当输入666时,进行四则运算,调用函数t()
if n==666:
    while True:
        result  = t()
        j= input()
        s= int(j)
        if s== result :
            print('right')
        else:
            print('error.,the answer is', result )
#当输入2333333时,进行制作题库
if n==2333333:
    test()

参考:https://www.cnblogs.com/zhouxiaochun/p/10567992.html

posted @ 2020-11-29 17:12  游浩贤  阅读(101)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3