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

 >  기사  >  백엔드 개발  >  Python을 사용하여 산술 문제를 무작위로 생성하는 방법

Python을 사용하여 산술 문제를 무작위로 생성하는 방법

PHPz
PHPz앞으로
2023-05-09 23:55:131564검색

1. 환경 준비

계산 문제를 무작위로 생성한 다음 무작위 모듈을 가져와야 합니다.

환경 설치: Python 3.8: 인터프리터, pycharm: 코드 편집기. 이번 내용은 매우 간단하며 별도의 모듈 설치가 필요 없습니다~

2. 메인 프로그램

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. 효과 표시

Python을 사용하여 산술 문제를 무작위로 생성하는 방법

위 내용은 Python을 사용하여 산술 문제를 무작위로 생성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제