登录
首页 >  文章 >  python教程

Python怎么实现随机生成算术题

来源:亿速云

时间:2024-04-05 13:54:20 112浏览 收藏

今天golang学习网给大家带来了《Python怎么实现随机生成算术题》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

1、环境准备

随机生成生成计算题,那我们便需要导入random模块。

环境安装:python 3.8: 解释器、pycharm: 代码编辑器。这次的内容很简单不需要安装什么模块,直接安装完Python可以直接使用的哈~

2、主程序

import random
def add():
    a=random.randint(0,10)
    b=random.randint(0,10)
    print(f"{a}+{b}=?")
    c=input(">")
    if a+b!=int(c):
        print("wrong!")
    else:
        print("right!")

def subtract():
    j = random.randint(0, 100)
    h = random.randint(0, 100)
    print(f"{j}-{h}=?")
    s = input(">")
    if j - h != int(s):
        print("wrong!")
    else:
        print("riht!")
def multiplication():
    x=random.randint(0,100)
    y=random.randint(0,100)
    print(f"{x}*{y}=?")
    z=input(">")
    if x*y!=int(z):
        print("wrong!")
    else:
        print("riht!")
def divide():
    l = random.randint(0, 100)
    m = random.randint(1, 100)
    print(f"{l}/{m}=?")
    o = input(">")
    if l / m != float(o):
        print("wrong!")
    else:
        print("riht!")
i=1
while i<=10:
    i+=1
    add()
    multiplication()
    subtrct()
    divide()

3、效果展示

Python怎么实现随机生成算术题

以上就是《Python怎么实现随机生成算术题》的详细内容,更多关于Python的资料请关注golang学习网公众号!

声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>