#=================================#
#加减法数字练习#
#=================================#
#time:2020-1-09
#author: chenshai
#=================================#
#代码部分
from randomimport randint, choice
def exam():
'用于出题,让用户作答'
#随机生成两个数字
nums = [randint(1,100)for i in range(2)]
#降序排列
nums.sort(reverse=True)
#随机加减法
op = choice('+-')
#计算正确答案
if op =='+':
result = nums[0] + nums[1]
else:
result = nums[0] - nums[1]
#用户作答,判断正误
prompt ='%s %s %s = ' % (nums[0],op,nums[1])
counter =0
while counter <3:
try:
answer =int(input(prompt))
except:
print()#打印回车
continue
if answer == result:
print('答对了...哎呦!不错呦!')
break
print('蠢货!这都不会...')
counter +=1
else:
print('正确答案:%s%s'% (prompt,result))
def main():
while 1:
exam()
try:
yn =input('Continue(y/n)?').strip()[0]
except IndexError:
continue
except(KeyboardInterrupt,EOFError):
yn ='n'
#取出第一个参数
if ynin 'nN':
print('\n Bye-bye')
break
if __name__ =='__main__':
main()