python 加减小游戏,python减小游戏,from operato


from operator import add,subfrom random import randint,choiceops={'+':add,'-':sub}MAXTRIES=2def doprob():    op=choice("+-")    nums=[randint(1,10) for i in range(2)]    nums.sort(reverse=True)    ans=ops[op](*nums)    pr='%d %s %d='%(nums[0],op,nums[1])    oops=0    while True:        try:            if int(input(pr))==ans:                print('correct')                break            if oops==MAXTRIES:                print('answer:',pr,ans)            else:                print("incorrect ...try again")                oops+=1        except ValueError as e:            print('input err',e)def main():    while True:        doprob()        try:            opt=input('again ? [y]').lower()            if opt and opt[0]=='n':                break        except EOFErroe as e:            breakif __name__=='__main__':    main()#该片段来自于http://byrx.net

评论关闭