(python3.7.x)
前置き
この動画のような問題をいくつか生成してみる
コード
雑に書いちゃったから後で見直すかも
ちなみに最大2桁
import random def get_num(): num_list = range(10) one1 = random.sample(num_list, 1)[0] one2 = random.sample(num_list[one1:], 1)[0] if one1 == one2 and 0 < one1: one1 -= 1 elif one1 == one2 and len(num_list) > one1: one2 += 1 return [one1, one2] amount = 10 max_ten1 = 9 for _ in range(amount): one1, one2 = get_num() ten1 = random.sample(range(1, max_ten1), 1)[0] ten2 = 0 if 1 < ten1 and ten1 < max_ten1: ten2 = random.sample(range(0, ten1), 1)[0] answer = (ten1 * 10 + one1) - (ten2 * 10 + one2) print('{}{}-{}{} = {}'.format(ten1, one1, ten2, one2, answer))
実行結果例
40-12 = 28 46-38 = 8 26-08 = 18 20-02 = 18 38-09 = 29 46-38 = 8 28-09 = 19 53-38 = 15 81-04 = 77 70-31 = 39