数学加减法

#1.随机生成两个100以内的数字
#2.随机选择加法或减法
#3.总是使用大的数字减去小的数字
#4.如果用户答错三次,程序给出正确答案

def add(x, y):
    return x + y


def sub(x, y):
    return x - y


def exam():
    num = [random.randint(1, 100) for i in range(2)]
    num.sort(reverse=True)
    op = random.choice('+-')

    if op == '+':
        opresult = add(*num)
    else:
        opresult = sub(*num)

    j = 0
    while j < 3:
        try:
            result = int(input('%s%s%s=' % (num[0], op, num[1])))
        except:
            continue

        if opresult == result:
            print('您赢了!')
            break
        else:
            print('您输了!')
            j += 1
    else:
        print('%s%s%s=%s' % (num[0], op, num[1], opresult))


def main():
    while True:
        print("############数学加减游戏#############")
        exam()
        try:
            propmt = '''您是否继续游戏(y/n):'''
            choice = input(propmt).strip().lower()[0]
        except:
            continue

        if choice not in 'yn':
            break

        if choice == 'n':
            exit()


if __name__ == '__main__':
    main()

  

猜你喜欢

转载自www.cnblogs.com/pslblog/p/10239008.html