简单写了一下,你看看行不行,python3.X
import random
opr = ['+','-','×','÷']
jg = '0'
print('Input "0000" Quit')
while True:
fh = random.randint(0, 3)
n1 = random.randint(1, 10)
n2 = random.randint(1, 10)
rjg = 0
if fh == 0:
rjg = n1 + n2
elif fh == 1:
n1,n2 = max(n1,n2),min(n1,n2)
rjg = n1 - n2
elif fh == 2:
rjg = n1 * n2
elif fh == 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)
rjg = int(n1 / n2)
print(n1, opr[fh], n2, '= ', end='')
jg = input()
if jg == '0000':
break
sr = int(jg)
if int(sr) == rjg:
print('right')
else:
print('error. the Tight answer is', rjg)
random模块用来随机产生操作数和运算符。
raw_input获取用户输入。
整个过程循环。