Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

my code works but pytest says that the output is not the same. can anyone assist?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    
    char_top = []
    char_bottom = []
    char_line = []
    char_answers = []
    for problem in problems:
        parts = problem.split()
        arg1 = parts[0]
        operator = parts[1]
        arg2 = parts[2]
        if operator not in ["+","-"]:
            return "Error: Operator must be '+' or '-'."
        if len(arg1) > 4 or len(arg2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        try:
            result = str(eval(problem))
        except:
            return "Error: Numbers must only contain digits."
        max_length = max(len(arg1), len(arg2)) + 2
        char_top.append(arg1.rjust(max_length))
        char_bottom.append(operator + " " + arg2.rjust(max_length - 2))
        char_answers.append(result.rjust(max_length))
        char_line.append('-' * max_length)
    arranged_problems = ""
    if char_top:
        arranged_problems += "    ".join(char_top) + "\n"
    if char_bottom:
        arranged_problems += "    ".join(char_bottom) + "\n"
    if char_line:
        arranged_problems += "    ".join(char_line) + "\n"
    if show_answers:
        if char_answers:
            arranged_problems += "    ".join(char_answers)
    return arranged_problems
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"],True)}')
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/124.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

AssertionError: '  3801      123\n-    2    +  49\n------    -----\n' 
             != '  3801      123\n-    2    +  49\n------    -----'

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

it’s not the same, you have an extra \n at the end of the string

what is the piece of code that adds the new line character?

hey, thanks for the help. yes i got rid of the ‘\n’ and the code works for the first 2 outputs, but now it has messed up the output of last two pieces. do you mind taking a look? i’m confused.

You would just need to check if it’s the last row and if it is, don’t add the newline.

THIS WORKED!! thankyou so much.

1 Like