Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Hi. I’m almost done w’ the arithmetic formatter, but the top operand still isn’t formatted quite right. Any help with this would be greatly appreciated.

Say I were to run this:

arithmetic_arranger(["3801 - 2", "123 + 49"])

Expected result:

  3801      123
-    2    +  49
------    -----

My result:

  3801     123 
-    2    +  49
------    -----


Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 4:
        return 'Error: Too many problems.'

    top_thing = ''
    bottom_thing = '\n'
    equals = '\n'
    print('Arithmetic Formatter\n\n\n\n')

    for problem in problems:

        largest = 0
        split = problem.split()
        for thing in split: 
        #I couldn't think of a better parameter name

            diff = 0
            if thing.isnumeric():
                if int(thing) > largest:
                    largest = int(thing)
                else:
                    diff = len(str(largest)) - len(str(thing))
            largest_len = len(str(largest))
            if not thing.isnumeric():
                if not split.index(thing) == 1:
                    return 'Error: Numbers must only contain digits.'
            space = ' '
            for num in range(diff):
                space += ' '
            formatted_thing = thing
            length = len(thing)
            sign = split[1]
            if sign != '+':
                if sign != '-':
                    return "Error: Operator must be '+' or '-'."
            if split.index(thing) == 2:
                if length == 1:
                        formatted_thing = sign + space + formatted_thing
                elif length == 2:
                    formatted_thing = sign + space + formatted_thing
                elif length == 3:
                    formatted_thing = sign + space + formatted_thing
                elif length == 4:
                    formatted_thing = sign + space + formatted_thing
                else:
                    return 'Error: Numbers cannot be more than four digits.'
                bottom_thing += formatted_thing + '    '
                #print(formatted_thing)
                if largest_len == 1:
                    equals += '---    '
                    #print('---')
                elif largest_len == 2:
                    equals += '----    '
                    #print('----')
                elif largest_len == 3:
                    equals += '-----    '
                    #print('-----')
                elif largest_len == 4:
                    equals += '------    '
                    #print('------')
            else:
                upper_space = ''
                print(diff)
                if diff == 1:
                    upper_space = ' '
                if length == 1:
                    if formatted_thing == '+' or '-':
                        sign = formatted_thing
                    else:
                        formatted_thing = '    ' + formatted_thing
                        #print(formatted_thing)
                        top_thing += upper_space + formatted_thing + '    '
                elif length == 2:
                    formatted_thing = '   ' + formatted_thing
                    #print(formatted_thing)
                    top_thing += upper_space + formatted_thing + '    '

                elif length == 3:
                    formatted_thing = '  ' + formatted_thing
                    #print(formatted_thing)
                    top_thing += upper_space + formatted_thing + '    '
                else:
                    formatted_thing = '  ' + formatted_thing
                    #print(formatted_thing)
                    top_thing += upper_space + formatted_thing + '   '
        print(diff, '\n\n')
        if show_answers:
            result = 0
            if sign == '+':
                result = int(split[0]) + int(split[2])
            else:
                result = int(split[0]) - int(split[2])

            if len(f'{result}') == 1:
                result = '    ' + f'{result}'
            elif len(f'{result}') == 2:
                result = '   ' + f'{result}'
            elif len(f'{result}') == 3:
                result = '  ' + f'{result}'
            elif len(f'{result}') == 4:
                result = ' ' + f'{result}'
            print(result)

    output = top_thing + bottom_thing + equals
    print('Input:\n', problems, ',', show_answers)
    print('\n\n\nOutput:')
    return output

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


Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project