首页 > 文章列表 > Python怎么实现随机生成算术题

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

Python
383 2023-05-17

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、效果展示