牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Pythonで足し算と引き算の式を任意の数だけ作成してみるメモ

息子の足し算のお勉強用(雑なコードだが目的は果たせるのでよしとする)

code

例:1~15 の数での足し算と引き算の式を10個作成する

from random import choice


def random_tashizan_and_hikizan(min, max, amount=10):
    exp_list = []
    for i in range(amount):
        is_plus = choice([True, False])
        a = choice(range(min, max + 1))

        if is_plus:
            b = choice(range(min, max + 1))
        else:
            b = choice(range(min, a + 1))

        exp_list.append(f'{a}{"+" if is_plus else "-"}{b}={a+b if is_plus else a-b}')

    return exp_list


print('\n'.join(random_tashizan_and_hikizan(1, 15, 10)))

結果例

6-2=4
15+3=18
4+3=7
10+4=14
8+3=11
7-7=0
7+12=19
11+12=23
10-3=7
10-1=9

Python Playground

https://www.online-python.com/4fXFr52GKk

Pythonで九九をランダムに任意の数だけ表示してみるメモ

息子の九九のお勉強用

code

from random import shuffle


def random_kuku(start, end, amount=1):
    kuku_list = []
    for i in range(start, end + 1):
        for j in range(2, 10):
            kuku_list.append(f"{i}*{j}={i*j}")

    if len(kuku_list) < amount:
        amount = len(kuku_list)

    shuffle(kuku_list)
    return kuku_list[0:amount]


print('\n'.join(random_kuku(1, 9, 10)))

結果例

2*4=8
4*5=20
1*9=9
7*3=21
4*6=24
9*8=72
2*5=10
4*8=32
5*9=45
6*8=48

Python Playground

www.online-python.com