Python练习(二)——分支语句及随机数模块的应用

 设计一个程序,帮助小学生练习10以内的加法

             详情:

                       - 随机生成加法题目;

                       - 学生查看题目并输入答案;

                       - 判别学生答题是否正确;

                       - 退出时,统计学生答题总数,正确数量及正确率(保留两位小数)。

智障型:

#!/usr/local/python3/bin/python3
import random
j = 0

for i in range(1,6):
        x = random.randint(1,10)
        print("第%d题" %(i))
        if x == 1:
                s = int(input(" 6 + 4 = ?"))
                if s == 10:
                        j += 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 2:
                s = int(input(" 5 + 2 = ?"))
                if s == 7:
                        j+=1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 3:
                s = int(input(" 9 + 1 = ?"))
                if s == 10:
                        j=j+1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
         elif x == 4:
                s = int(input(" 5 + 4 = ?"))
                if s == 9:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 5:
                s = int(input(" 2 + 2 = ?"))
                if s == 4:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 6:
                s = int(input(" 5 + 9 = ?"))
                if s == 14:
                        j = j + 1
                else:
                        print("回答错误,请继续加油!")
        elif x == 7:
                s = int(input(" 5 + 2 = ?"))
                if s == 7:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 8:
                s = int(input(" 9 + 9 = ?"))
                if s == 18:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        elif x == 9:
                s = int(input(" 4 + 3 = ?"))
                if s == 7:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
        else:
                s = int(input(" 1 + 2 = ?"))
                if s == 3:
                        j = j + 1
                        print("回答正确")
                else:
                        print("回答错误,请继续加油!")
accuracy = float(j/5)*100
print("测试结束,答对了%d道题,正确率为%.2f" %(j,accuracy))

更新一种很简单的代码:

看了老师的代码,服了自己,哈哈哈哈

import random

rightCount = 0
print("小学生算术测试系统".center(50,"*"))
totalCount = int(input("请输入所需要的题目数量:"))
for count in range(totalCount):
    num_1 = random.randint(1,10)
    num_2 = random.randint(1,10)
    sum_ = num_1 + num_2

    print("第%d题: %d + %d = " %(count+1,num_1,num_2),end = ' ')
    userRes = int(input())
    if userRes == sum_ :
        rightCount += 1
        print("OK")
    else :
        print("wrong")
else:
    print("测试结果".center(50,'*'))
    print("答对数量: %d" %(rightCount))
    print("正确率: %.2f%%" %(rightCount/totalCount*100))

猜你喜欢

转载自blog.csdn.net/weixin_41179709/article/details/81545950