一个用Python3做的算数游戏

下面是一个python的算数游戏编程

import random
import time
cho = ('+', '-', '*', '/')
ddd = (2,3,4,5,6,10,20,15,12,30,60)
a = input("The level(Beginners, advanced, difficult or 1, 2, 3, 4):")
a = a.upper()
score = 0
if a == '1' or a == "BEGINNERS":
    x = 1
elif a == '2':
    x = 2
elif a == '3' or a == "ADVANCED":
    x = 3
elif a == '4' or a == "DIFFICULT":
    x = 4
else:
    print("Level not available.")
    quit()
print("Input 2147483647 to exit")
if x == 1:
    time1 = time.time()
    for n in range(10):
        a1 = random.randint(0, 10)
        a2 = random.randint(0, 10)
        ti = str(a1) + "+" + str(a2)
        answer = eval(ti)
        ti += "="
        ia = int(input(ti))
        if ia == 2147483647:
            quit()
        if ia == answer:
            score += 10
            print(" √")
        else:
            print(" X")
    time2 = time.time()
    timeUse = int(round(time2))-int(round(time1))
    print("Mark: ",score,".\ntime: ",timeUse,'s')
elif x == 2:
    time1 = time.time()
    for n in range(10):
        a1 = random.randint(0, 10)
        a2 = random.randint(0, 10)
        le = random.choice(cho)
        if le == '-' and a2 > a1:
            tmmm = a2
            a2 = a1
            a1 = tmmm
        if le == '/':
            a1 = 60*random.randint(1, 2)
            a2 = random.choice(ddd)
        if le == '/' and a2 == 0:
            n -= 1
            continue
        ti = str(a1) + le + str(a2)
        answer = eval(ti)
        ti += "="
        ia = int(input(ti))
        if ia == 2147483647:
            quit()
        if ia == answer:
            score += 10
            print(" √")
        else:
            print(" X")
    time2 = time.time()
    timeUse = int(round(time2))-int(round(time1))
    print("Mark: ",score,".\ntime: ",timeUse,'s')
elif x == 3:
    time1 = time.time()
    for n in range(10):
        a1 = random.randint(0, 100)
        a2 = random.randint(0, 100)
        le = random.choice(cho)
        if le == '*' :
            a1 %= 10
            a2 %= 11
        if le == '/' :
            a1 = random.choice(ddd)
            a2 = 1
        if le == '/' and a2 == 0:
            n -= 1
            continue
        if le == '-' and a2 > a1:
            tmmm = a2
            a2 = a1
            a1 = tmmm
        ti = str(a1) + le + str(a2)
        answer = eval(ti)
        ti += "="
        ia = int(input(ti))
        if ia == 2147483647:
            quit()
        if ia == answer:
            score += 10
            print("√")
        else:
            print("X")
    time2 = time.time()
    timeUse = int(round(time2))-int(round(time1))
    print("Mark: ",score,".\ntime: ",timeUse,'s')
else:
    time1 = time.time()
    for n in range(10):
        a1 = random.randint(0, 100)
        a2 = random.randint(0, 100)
        le = random.choice(cho)
        if le == '-' and a2 > a1:
            tmmm = a2
            a2 = a1
            a1 = tmmm
        if le == '*':
            a1 %= 40
            a2 %= 25
        if le == '/':
            a1 = a2 * random.randint(0,10)
        if le == '/' and a2 == 0:
            n -= 1
            continue
        ti = str(a1) + le + str(a2)
        answer = eval(ti)
        ti += "="
        ia = int(input(ti))
        if ia == 2147483647:
            quit()
        if ia == answer:
            score += 10
            print(" √")
        else:
            print(" X")
    time2 = time.time()
    timeUse = int(round(time2))-int(round(time1))
    print("Mark: ",score,".\ntime: ",timeUse,'s')

使用方法:
首先选择级别(beginners、advanced、difficult、1、2、3), 无需区分大小写。
然后做题
10道题做完后就会出成绩和作答时间。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值