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

How to randomly generate arithmetic problems using Python

PHPz
Release: 2023-05-09 23:55:13
forward
1635 people have browsed it

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()
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template