import random
input('按任意键开始游戏')
while 1==1:
d=input('请问你想要连续答对几道通关:')
while not d.isdigit():
d=input('请重输:')
c = int(d)
hh=True
while hh:
g=input('请输入你想要的数字范围:(请用\',\'间隔开,且必须为半角输入)')
while int(g.count(','))!=1:
g=input('请输入你想要的数字范围:(请用\',\'间隔开,且必须为半角输入)')
while g.index(',') ==0 or g.index(',') ==len(g)-1:
g=input('请输入你想要的数字范围:(请用\',\'间隔开,且必须为半角输入)')
h=g.index(',')
if not g[:h].isdigit():
continue
if not g[h+1:].isdigit():
continue
k=int(g[:h])
l=int(g[h+1:])
if k<l:
hh=False
else:
input('左边的数字应小于右边的数字')
i=0
while i < c:
e=random.randint(1,2)
a=random.randint(k,l)
b=random.randint(k,l)
if e == 1:
answer=input('%d+%d='%(a,b))
while not answer.isdigit():
if answer!='':
if answer[0] in '-':
answe=answer[1:]
if not answe.isdigit():
answer=input('请重输:')
continue
else:
break
answer=input('请重输:')
if int(answer) ==a+b:
print('答对了,进入下一题。')
i+=1
else:
print('错误,重新开始')
f=open("错题本.txt",'a+')
f.write('%d+%d='%(a,b)+'\n')
f.close()
i=0
if e == 2:
answer=input('%d-%d='%(a,b))
while not answer.isdigit():
if answer!='':
if answer[0] in '-':
answe=answer[1:]
if not answe.isdigit():
answer=input('请重输:')
continue
else:
break
answer=input('请重输:')
if int(answer) ==a-b:
print('答对了,进入下一题。')
i+=1
else:
print('错误,重新开始')
f=open("错题本.txt",'a+')
f.write('%d+%d='%(a,b)+'\n')
f.close()
i=0
print('恭喜你通关。')
while 1==1:
sd=input('还要再来吗?(1.是 2.否):')
if sd=='1':
print('\n'*40)
break
if sd=='2':
exit()