python-primary school students random operation test

Topic implementation requirements:

You can customize the number of questions and the size range of numbers. There are prompts for mistakes. If you make a mistake, the correct answer will be displayed below.

After completing the specified required questions, it will show how many questions you have practiced in total, how many questions are correct, how many questions are wrong, and the correct rate and time-consuming!

code:

import random,time 
print("Primary school students four arithmetic test practice (enter the oo button to directly exit the arithmetic learning)") 
sys = ['+', '-', '*'] 
ans = "" 
i=1 
t=0 
l =int(input("How many questions do you want to practice?")) 
a1 = int(input("What range do you want to practice in? 1--a1, enter the value of a1 you want:") ) 
while ans != "00": 
    if i<=l: 
        start = time.time() 
        add1 = random.randint(1, a1) 
        add2 = random.randint(1, a1) 
        sy = random.randint(0, 2) 
        li = str(add1)+sys[sy]+str(add2) 
        # eval function: the result of calculating the string! 
        val = eval(li) 
        print("Exercise %d: %s=" % (i, li)) 
        ans = input("Answer %d: "%(i)) 
        if ans == '00':
             t = t + 1 
             i = i + 1 
             print("your answer is right!") 
        else: 
            i = i + 1 
            print("your answer is error ,the right answer is %d" % val) 
    else: 
        end=time .time() 
        ave = t / l * 100 
        et = l - t 
        print("End of exercise") 
        print("You have practiced a total of %d questions this time, there are %d correct questions and %d wrong ones Road, the accuracy rate is %d%%, and the total time is %.2f seconds" % (l, t, et, ave, end-start)) 
        break


Run the screenshot: 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/127212705