Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

print(f’\n{arithmetic_arranger([“24 + 85215”, “3801 - 2”, “45 + 43”, “123 + 49”])}') doesn’t return ‘Error: Numbers cannot be more than four digits.’ as it should be

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    #check điều kiện 1

    if len(problems) > 5:
        return 'Error: Too many problems.'
    #check điều kiện 2

    for problem in problems:
        if '+' in problem:  
            pass
        elif '-' in problem:
            pass
        else:
            return "Error: Operator must be '+' or '-'."
    #check điều kiện 3 + 4 chia dấu và số
    numbers = []   
    signs = []
    for problem in problems:
        splitnum = problem.split()
        numbers.append(splitnum[0])
        numbers.append(splitnum[2])
        signs.append(splitnum[1])
    for number in numbers:
        if not number.isdigit():
            return 'Error: Numbers must only contain digits.'
    for number in numbers:
        if len(number) > 4:
            return 'Error: Numbers cannot be more than four digits.'

    #đặt các tập
    
        answers = []
        toprow = ''
        bottomrow = ''
        dash = ''
        answerrow = ''
        for i in range (0, len(numbers), 2):
            num1 = int(numbers[i])
            num2 = int(numbers[i+1])
            sign = signs[i//2]
            if sign == '+':
                answer = num1 + num2
            else:
                answer = num1 - num2
            answers.append(answer)

            spacewidth = max(len(numbers[i]), len(numbers[i+1])) + 2
            toprow += numbers[i].rjust(spacewidth)
            bottomrow += sign + numbers[i+1].rjust(spacewidth-1)
            dash += '-' * spacewidth
        #spacing
            if i < len(numbers) - 2:
                toprow += ' ' * 4
                bottomrow += ' ' * 4
                dash += ' ' * 4
        for i in range(len(answers)):
            spacewidth = max(len(numbers[i*2]),len(numbers[i*2+1])) + 2     #spacing đáp án (numbers[0] + numbers[1]=answers[1])
            answerrow += str(answers[i]).rjust(spacewidth)
            if i < len(answers) - 1:
                answerrow += ' '*4
        #show_answers true và false
        if show_answers == True:
            arrangedproblems = '\n'.join((toprow, bottomrow, dash, answerrow))
        else:
            arrangedproblems = '\n'.join((toprow, bottomrow, dash))


        return arrangedproblems

print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 OPR/115.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

what part of your code is checking for that?

it should be this part

add print(number) in the loop, does it check all the numbers?

1 Like

oh it doesn’t but i wonder why it doesn’t work like the code checking for the digits ?

oh wait i see why, i accidentally add the next part of my code into the loop as well while trying to fix other bug

good find! sometimes some debugging will help you figure out where is the point of failure!

1 Like