Computation drill python (extension)

This time, we will expand the function of "Calculation drill python" that I wrote last time. The functions to be added are as follows.

Extensions

・ Addition of problem number ・ Increased the number of questions to 100


First of all, I will add the question number. Since the code becomes complicated here, we will summarize the process of outputting the problem and answer in the form of a function. The code is below.

Problem number additional code

Output function in question


def question_out_txt(): #Function to output the problem
    for cnt in range(10):
        num1 = random.randint(1,9)
        num2 = random.randint(1,9)
        if (cnt + 1) / 10 < 1: #If the problem number is 1 digit
            question = "(" + str(cnt + 1) + ")  " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        else :
            question = "(" + str(cnt + 1) + ") " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        answers.append(num1 + num2)
        f.write(question + "\n")

Output function of the answer


def answer_out_txt(): #Function that outputs the answer
    f.write("\nA. ")
    for i,answer in enumerate(answers,1):
        out_ans = "(" + str(i) + ") "  + \
                  str(answer) + " "
        f.write(out_ans)

The output function of the answer uses the enumerate function </ b> to add the index. This function gets the index of an iterable object. I set the starting point to 1 and set the question number.

Whole code

Calculation drill


import random

answers = []

def question_out_txt(): #Function to output the problem
    for cnt in range(10):
        num1 = random.randint(1,9)
        num2 = random.randint(1,9)
        if (cnt + 1) / 10 < 1:
            question = "(" + str(cnt + 1) + ")  " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        else :
            question = "(" + str(cnt + 1) + ") " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        answers.append(num1 + num2)
        f.write(question+"\n")

def answer_out_txt(): #Function that outputs the answer
    f.write("\nA. ")
    for i,answer in enumerate(answers,1):
        out_ans = "(" + str(i) + ") "  + \
                  str(answer) + " "
        f.write(out_ans)

with open("keisan.txt","w") as f:
    question_out_txt()
    answer_out_txt()

Output result


(1)  7 + 1 = 
(2)  8 + 8 = 
(3)  5 + 2 = 
(4)  9 + 7 = 
(5)  5 + 9 = 
(6)  1 + 6 = 
(7)  4 + 5 = 
(8)  6 + 2 = 
(9)  5 + 3 = 
(10) 2 + 7 = 

A. (1) 8 (2) 16 (3) 7 (4) 16 (5) 14 (6) 7 (7) 9 (8) 8 (9) 8 (10) 9 

Increased to 100 questions

Next, we will increase the number of questions. The format is 5x20, but I want to align the indents neatly, so I will align the indents using a half-width space. The code is below.

Problem number increase code

Problem output


def question_out_txt(): #Function to output the problem
    for cnt in range(100):
        num1 = random.randint(1,9)
        num2 = random.randint(1,9)
        if (cnt + 1) / 10 < 1: #For 1 digit
            question = "(" + str(cnt + 1) + ")  " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        else :
            question = "(" + str(cnt + 1) + ") " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        answers.append(num1 + num2)
        if (cnt + 1) % 5 == 0:
            f.write(question + "\n")
        else:
            f.write(question + " ")

Here, the indentation shifts depending on whether the question number is one or two digits, so the number of half-width spaces is specified in the conditional branch to align the indentation. When outputting the problem, the problem is broken at a multiple of 5.

Answer output


def answer_out_txt(): #Function that outputs the answer
    f.write("\nA.\n")
    for i,answer in enumerate(answers,1):
        if (i / 10 < 1) and (answer / 10 < 1): #1 digit number,1 digit answer
            out_ans = "(" + str(i) + ")  " + \
                      str(answer) + "  "
        elif (i / 10 < 1) and (answer / 10 >= 1): #1 digit number,The answer is 2 digits
            out_ans = "(" + str(i) + ")  " + \
                      str(answer) + " "
        elif (i / 10 >= 1) and (answer / 10 < 1): #2 digits number,1 digit answer
            out_ans = "(" + str(i) + ") " + \
                      str(answer) + "  "
        else:                                     #2 digits number,The answer is 2 digits
            out_ans = "(" + str(i) + ") " + \
                      str(answer) + " "

        if i % 5 == 0: #Line breaks for each question number that is a multiple of 5
            f.write(out_ans + "\n")
        else:
            f.write(out_ans + " ")

The point is the processing of the answer output. Since the number of digits of each of the question number and the answer is one digit or two digits, conditional branching is 2 ^ 2 and there are four ways </ b>. Again, the question number breaks every multiple of 5.

Whole code

Calculation drill


import random

answers = []

def question_out_txt(): #Function to output the problem
    for cnt in range(100):
        num1 = random.randint(1,9)
        num2 = random.randint(1,9)
        if (cnt + 1) / 10 < 1: #If less than 2 digits
            question = "(" + str(cnt + 1) + ")  " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        else :
            question = "(" + str(cnt + 1) + ") " + \
                       str(num1) + " + " + \
                       str(num2) + " = "
        answers.append(num1 + num2)
        if (cnt + 1) % 5 == 0:
            f.write(question + "\n")
        else:
            f.write(question + " ")

def answer_out_txt(): #Function that outputs the answer
    f.write("\nA.\n")
    for i,answer in enumerate(answers,1):
        if (i / 10 < 1) and (answer / 10 < 1): #1 digit number,1 digit answer
            out_ans = "(" + str(i) + ")  " + \
                      str(answer) + "  "
        elif (i / 10 < 1) and (answer / 10 >= 1): #1 digit number,The answer is 2 digits
            out_ans = "(" + str(i) + ")  " + \
                      str(answer) + " "
        elif (i / 10 >= 1) and (answer / 10 < 1): #2 digits number,1 digit answer
            out_ans = "(" + str(i) + ") " + \
                      str(answer) + "  "
        else:                                     #2 digits number,The answer is 2 digits
            out_ans = "(" + str(i) + ") " + \
                      str(answer) + " "

        if i % 5 == 0: #Line breaks for each question number that is a multiple of 5
            f.write(out_ans + "\n")
        else:
            f.write(out_ans + " ")

with open("keisan.txt","w") as f: #Open text file
    question_out_txt()
    answer_out_txt()

Output result


(1)  5 + 8 =  (2)  2 + 4 =  (3)  8 + 7 =  (4)  2 + 4 =  (5)  7 + 9 = 
(6)  8 + 8 =  (7)  8 + 5 =  (8)  7 + 8 =  (9)  2 + 1 =  (10) 3 + 1 = 
(11) 8 + 1 =  (12) 7 + 2 =  (13) 9 + 8 =  (14) 6 + 2 =  (15) 3 + 4 = 
(16) 9 + 9 =  (17) 5 + 7 =  (18) 7 + 5 =  (19) 4 + 9 =  (20) 4 + 8 = 
(21) 7 + 3 =  (22) 1 + 7 =  (23) 4 + 5 =  (24) 1 + 6 =  (25) 5 + 1 = 
(26) 4 + 7 =  (27) 9 + 4 =  (28) 8 + 4 =  (29) 5 + 4 =  (30) 3 + 5 = 
(31) 2 + 7 =  (32) 8 + 1 =  (33) 5 + 6 =  (34) 7 + 6 =  (35) 2 + 8 = 
(36) 9 + 9 =  (37) 5 + 3 =  (38) 6 + 1 =  (39) 3 + 1 =  (40) 3 + 2 = 
(41) 7 + 7 =  (42) 2 + 5 =  (43) 9 + 1 =  (44) 7 + 9 =  (45) 6 + 5 = 
(46) 5 + 8 =  (47) 8 + 4 =  (48) 2 + 2 =  (49) 3 + 2 =  (50) 3 + 9 = 
(51) 6 + 4 =  (52) 7 + 9 =  (53) 5 + 5 =  (54) 9 + 5 =  (55) 6 + 5 = 
(56) 9 + 6 =  (57) 2 + 2 =  (58) 1 + 7 =  (59) 9 + 7 =  (60) 5 + 5 = 
(61) 9 + 5 =  (62) 3 + 1 =  (63) 2 + 8 =  (64) 4 + 1 =  (65) 6 + 6 = 
(66) 6 + 6 =  (67) 4 + 6 =  (68) 8 + 8 =  (69) 7 + 1 =  (70) 2 + 8 = 
(71) 5 + 1 =  (72) 8 + 8 =  (73) 4 + 8 =  (74) 2 + 3 =  (75) 5 + 2 = 
(76) 8 + 7 =  (77) 6 + 8 =  (78) 7 + 6 =  (79) 2 + 1 =  (80) 4 + 9 = 
(81) 9 + 3 =  (82) 6 + 5 =  (83) 5 + 7 =  (84) 7 + 7 =  (85) 9 + 6 = 
(86) 9 + 9 =  (87) 8 + 6 =  (88) 2 + 4 =  (89) 7 + 6 =  (90) 9 + 6 = 
(91) 8 + 2 =  (92) 5 + 3 =  (93) 3 + 3 =  (94) 2 + 7 =  (95) 4 + 2 = 
(96) 3 + 6 =  (97) 6 + 6 =  (98) 8 + 8 =  (99) 1 + 9 =  (100) 8 + 1 = 

A.
(1)  13  (2)  6   (3)  15  (4)  6   (5)  16 
(6)  16  (7)  13  (8)  15  (9)  3   (10) 4  
(11) 9   (12) 9   (13) 17  (14) 8   (15) 7  
(16) 18  (17) 12  (18) 12  (19) 13  (20) 12 
(21) 10  (22) 8   (23) 9   (24) 7   (25) 6  
(26) 11  (27) 13  (28) 12  (29) 9   (30) 8  
(31) 9   (32) 9   (33) 11  (34) 13  (35) 10 
(36) 18  (37) 8   (38) 7   (39) 4   (40) 5  
(41) 14  (42) 7   (43) 10  (44) 16  (45) 11 
(46) 13  (47) 12  (48) 4   (49) 5   (50) 12 
(51) 10  (52) 16  (53) 10  (54) 14  (55) 11 
(56) 15  (57) 4   (58) 8   (59) 16  (60) 10 
(61) 14  (62) 4   (63) 10  (64) 5   (65) 12 
(66) 12  (67) 10  (68) 16  (69) 8   (70) 10 
(71) 6   (72) 16  (73) 12  (74) 5   (75) 7  
(76) 15  (77) 14  (78) 13  (79) 3   (80) 13 
(81) 12  (82) 11  (83) 12  (84) 14  (85) 15 
(86) 18  (87) 14  (88) 6   (89) 13  (90) 15 
(91) 10  (92) 8   (93) 6   (94) 9   (95) 6  
(96) 9   (97) 12  (98) 16  (99) 10  (100) 9  

Looking back

It's been a long time, but this time we expanded the functionality by assigning question numbers and increasing the number of questions. I had a hard time thinking about conditional branching to align the indentation. But the question is, what do you do with indentation when the number of problems is arbitrary </ b>? From now on, I would like to solve it while expanding the functions.