Use python to automatically generate elementary school addition and subtraction programs

Hello friends, what I bring to you today is: use python as an automatic question-generating program.

This program can randomly generate two-digit addition and subtraction programs.

Look at the code first:

import time
import random


ops=['+','-']
random.shuffle(ops)
ops1=ops[0]
numbers=1
numbers1=0
while True:
    add1=random.randint(1,99)
    add2=random.randint(1,99)
    ops1=ops[0]
    eq=str(add1)+ops[0]+str(add2)
    val=eval(eq)
    if add1-add2<0:
        continue
    print('第',numbers,'题')
    time.sleep(1)
    print(add1,ops1,add2,'=')
    ask=int(input('答案是几:'))
    if ask==val:
        print('恭喜你,答对了')
        print()
        numbers+=1
        random.shuffle(ops)
    else:
        print('答错了,正确答案是',val)
        print()
        numbers+=1
        random.shuffle(ops)

Let's see the effect:

As you can see, the program automatically generates several questions about two-digit addition and subtraction, as long as you enter the correct answer, the program will tell you that you got it right.

Well, the content of this article is here, thank you for watching! 

Guess you like

Origin blog.csdn.net/hu20100913/article/details/126545150