这篇文章主要介绍“Python怎么实现随机生成算术题”,在日常操作中,相信很多人在Python怎么实现随机生成算术题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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、效果展示