#Python代码
from random import *
from time import *
fuhao = input('1-加法,2-乘法,否则退出')
n = int(input('位数>>>'))
m = int(input('题目数量(输入0则无限)>>>'))
t1,count = time(),0
if fuhao == '1':
if m == 0:
while True:
a,b = randint(10**(n-1),10**n-1),randint(10**(n-1),10**n-1)
try:
ans = int(input('%d+%d=' %(a,b)))
if a+b == ans:
count += 1
t2 = time()-t1
else:
print('错误,正确答案是',a+b)
break
except:
print('输入错误,跳过')
else:
for i in range(m):
a,b = randint(10**(n-1),10**n-1),randint(10**(n-1),10**n-1)
try:
ans = int(input('%d+%d=' %(a,b)))
if a+b == ans:
count += 1
else:
print('错误,正确答案是',a+b)
except:
print('输入错误,跳过')
t2 = time()-t1
elif fuhao == '2':
if m == 0:
while True:
try:
a,b = randint(10**(n-1),10**n-1),randint(10**(n-1),10**n-1)
ans = int(input('%d*%d=' %(a,b)))
if a*b == ans:
count += 1
t2 = time()-t1
else:
print('错误,正确答案是',a*b)
break
except:
print('输入错误,跳过')
else:
for i in range(m):
try:
a,b = randint(10**(n-1),10**n-1),randint(10**(n-1),10**n-1)
ans = int(input('%d*%d=' %(a,b)))
if a*b == ans:
count += 1
else:
print('错误,正确答案是',a*b)
except:
print('输入错误,跳过')
t2 = time()-t1
else:
quit()
print('你答对了%d道题,用时%.2f秒' % (count,t2))