实现的功能:1,面向做题人:即时生成四则运算,等待做题人输入结果,然后判断结果是否正确,并给出反馈。
2,面向老师:按照老师输入的题目数量,生成相应数目的四则运算题目,并给出答案。
代码:
import random
def szys():
#即时四则远算函数
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():
#生成题库的函数
sym = ['+', '-', '×', '÷']
print('请输入所需要的题目数量')
n = int(input())
result = []
m = 0
while m <= (n - 1):
print(m + 1, end='、')
result.append(szys())
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())
if n == 1:
while True:
result = szys()
j = input()
s = int(j)
if s == result:
print('right')
else:
print('error.,the answer is', result)
if n == 2:
test()
程序运行展示: