Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

  1. arithmetic_arranger([“3 + 855”, “988 + 40”], True) should return 3 988\n+ 855 + 40\n----- -----\n 858 1028.
  2. arithmetic_arranger([“32 - 698”, “1 - 3801”, “45 + 43”, “123 + 49”, “988 + 40”], True) should return 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028.

my code doesnt pass these two tests. It looks right on the output. Can anyone help please?

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    solution = []
    data = []
    operators = []

    if len(problems) > 5:
        return 'Error: Too many problems.'

    for error in problems:
        lagos = error.split()
        operators.append(lagos[1])

    for operator in operators:
        if operator in ['*','/']:
            return "Error: Operator must be '+' or '-'." 

    for error in problems:
        lagos = error.split()
        data.append(lagos[0])
        data.append(lagos[2])
        
        for number in data:
            if not number.isdigit():
                return 'Error: Numbers must only contain digits.'
            elif len(number)>4:
                return 'Error: Numbers cannot be more than four digits.'

    toprow = ''
    bottomrow = ''
    lines = ''
    answerr = ''

    for i in range(0, len(data), 2):
        dig1 = int(data[i])
        dig2 = int(data[i+1])
        operator = operators[i//2]
        if operator == '+':
            result = dig1 + dig2
        else:
            result = dig1 - dig2
        solution.append(result)

        space = max(len(data[i]),len(data[i+1])) + 2
     
        toprow += data[i].rjust(space)
        bottomrow += operator + data[i+1].rjust(space - 1)
        lines += '-'*space
        
        if i != len(data) - 2:
            toprow += ' '*4
            bottomrow += ' '*4   
            lines += ' '*4
    
    for i in range(len(solution)):
        space = max(len(data[2*i]),len(data[(2*i)+1])) + 2
        answerr += str(solution[i]).rjust(space) 
    
        if i != len(data) - 1:
            answerr += ' '*4 

    if show_answers:
        arrangedp = '\n'.join((toprow,bottomrow,lines, answerr))
    else:
        arrangedp = '\n'.join((toprow,bottomrow,lines,))    

    return arrangedp


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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

There appear to be few additional spaces at the end of last line. Wrapping some additional character around the lines makes it easily visible, ie:

for line in arithmetic_arranger(["3 + 855", "988 + 40"], True).splitlines():
    print(f'"{line}"')

is there a method to remove extra lines at the end of a string? i tried using .strip but that also removes the empty spaces i need.

Yes, there is a method to remove EDIT: spaces at the end of a string

1 Like

Thank you. All i had to was add rstrip to the end of one thing lol.

1 Like