一个用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道题做完后就会出成绩和作答时间。

你可能感兴趣的:(一个用Python3做的算数游戏)