")ifa+b!=int(c):print(&q"/> ")ifa+b!=int(c):print(&q">

Home  >  Article  >  Backend Development  >  How to randomly generate arithmetic problems using Python

How to randomly generate arithmetic problems using Python

PHPz
PHPzforward
2023-05-09 23:55:131604browse

1. Environment preparation

Randomly generate calculation questions, then we need to import the random module.

Environment installation: python 3.8: interpreter, pycharm: code editor. The content this time is very simple and there is no need to install any modules. You can use it directly after installing Python~

2. Main program

import random
def add():
    a=random.randint(0,10)
    b=random.randint(0,10)
    print(f"{a}+{b}=?")
    c=input(">")
    if a+b!=int(c):
        print("wrong!")
    else:
        print("right!")

def subtract():
    j = random.randint(0, 100)
    h = random.randint(0, 100)
    print(f"{j}-{h}=?")
    s = input(">")
    if j - h != int(s):
        print("wrong!")
    else:
        print("riht!")
def multiplication():
    x=random.randint(0,100)
    y=random.randint(0,100)
    print(f"{x}*{y}=?")
    z=input(">")
    if x*y!=int(z):
        print("wrong!")
    else:
        print("riht!")
def divide():
    l = random.randint(0, 100)
    m = random.randint(1, 100)
    print(f"{l}/{m}=?")
    o = input(">")
    if l / m != float(o):
        print("wrong!")
    else:
        print("riht!")
i=1
while i<=10:
    i+=1
    add()
    multiplication()
    subtrct()
    divide()

3. Effect display

How to randomly generate arithmetic problems using Python

The above is the detailed content of How to randomly generate arithmetic problems using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete