python生成随机数给小朋友做加乘除的计算练习

 


介绍

提示用python写了一个测试小学生算术(加、乘、除)的程序,可以让小朋友自己做然后看看对了几题,


 

一、代码

python的运行环境

 

import random

 

##a = random.randint(0,100)

##b = random.randint(0,100)

##print(a,"*",b,"=")

##temp = input("")

##c1 = a * b  #转换数据类型:str转换为int

##print(c1)

 

#没事练一练,小朋友自己做,大人可以偷懒

count = 15 #总共的题目数,5道加法,5道乘法,5道除法

cont  = 0 #做对的计数

while count >0:

    a = random.randint(0,100) #100以内的随机数

    b = random.randint(0,100) #100以内的随机数

    c1 = random.randint(0,10) #10 以内的随机数

    c = a * c1 #除法的被除数,所有的除法都是整数除法

    if(count<=10):

        if(count<=5):#最后五道除法

            print(c,"/",c1,"=")

            temp = input("")

            c1 = a

        else:#中间五道乘法

            print(a,"*",b,"=")

            temp = input("")

            c1 = a * b 

    else:#开始五道加法

        print(a,"+",b,"=")

        temp = input("")

        c1 = a + b 

    if int(temp) == int(c1):

        print("对了")

        cont=cont+1

        ##break

    else:

        if int(temp) != int(c1):

            print("错了")

            print("正确答案为:",c1)

    count = count - 1

print ("做完了")

print ("对了",cont,"道")

print ("错了",15-cont,"道")

 

2.测试图


总结

可以在小朋友做的时候自己看手机,最后看结果,带孩子非常方便,以后有空写一个UI界面的计算练习。