#!/usr/bin/env pythonfrom operator Import Add, Subfrom random import randint, Choiceops = {' + ': Add, '-': sub}maxtries = 2 Def doprob (): op = Choice (' +-') Nums = [Randint (1,10) for I in range (2)] Nums.sort (reverse=true) ans = ops[o P] (*nums) PR = '%d%s%s = '% (Nums[0], op, nums[1]) oops = 0 while True:try:if int (raw_inpu T (pr)) = = ans:# #只有正确作答才跳出循环 print ' correct ' break if oops = = maxtries:# #第二次给出正确答案 , but still the title print ' Sorry ... the answer is\n%s%d '% (pr, ans) else:print ' Incorrec T ... try again ' oops + = 1 except (Keyboardinterrupt, Eoferror, ValueError): print ' Invalid input ... try again ' def main (): While True:doprob () try:opt = Raw_input (' Aga In? [y] '). Lower () if opt and opt[0] = = ' n ': Break except (Keyboardinterrupt, Eoferror): Breakif __name__ = = ' __main__ ': Main ()
"Python" example-answer system