I dont know whats wrong with my Arithmetic formatter

So i have been stuck on this problem for a long time I’m neary done but my code seems to mess up at this problem. [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]

def arithmetic_arranger(problems, answer = False):
    
    #sets up list to be used later
    over_list = list()
    middel_list = list()
    lower_list = list()
    the_main_list = problems

    #checks if there is more then 4 problems
    if len(the_main_list) > 5:
        return('Error: Too many problems.')
    
    #checks if the input is numbers and not charachters
    for one_cal in the_main_list:
        one_problem = one_cal.split()
        try: 
            one = int(one_problem[0])
            two = int(one_problem[2])
        except: 
            return("Error: Numbers must only contain digits.")

        #checks if the problem is - or +
        thing = one_problem[1] == '-' or one_problem[1] == '+'
        if not thing:
            return("Error: Operator must be '+' or '-'.")
        lengt = len(one_problem[0]) < 5 and len(one_problem[2]) < 5

        #checks if the problem is longer then 4 digets
        if not lengt:
            return('Error: Numbers cannot be more than four digits.')
    #Calcuats the aswers if requested
    aswer_list = list()
    if answer:
        for x in the_main_list:
            y = x.split()
            number_one = int(y[0])
            number_two = int(y[2])
            if y[1] == '+':
                asnwer3 = number_one + number_two
            elif y[1] == '-':
                asnwer3 = number_one - number_two
            if asnwer3 < 0:
                aswer_with_backspace = ' ' + str(asnwer3)
                aswer_list.append(aswer_with_backspace)
            else:
                aswer_with_backspace = '  ' + str(asnwer3)
                aswer_list.append(aswer_with_backspace)
    #Splits the problems and add the nessery spaces
    for x in problems:
        y = x.split()
        if len(y[0]) < 1 and len([2]) < 1:
            lengt_of_under = 4

        elif len(y[0]) >= len(y[2]):
            lengt_of_under = len(y[0]) + 2

        elif len(y[0]) <= len(y[2]):
            lengt_of_under = len(y[2]) + 2
        
        backspace_over = lengt_of_under - len(y[0])
        adfada = ' ' * backspace_over
        lower_things = '-' * lengt_of_under
        lower_space_numbers = lengt_of_under - len(y[2]) - 1
        lower_with_things = ' '*lower_space_numbers
        middel_list.append(y[1] + lower_with_things + y[2])
        over_list.append(adfada+y[0])
        lower_list.append(lower_things)

    #Arranges the list to be the way that is requsted
    Arragen = [over_list,middel_list, lower_list,  aswer_list]
    arranged_problems = ("\n".join(map("    ".join, Arragen)))
    return arranged_problems

Challenge: Arithmetic Formatter

Link to the challenge:

Your error on my machine (slightly rearranged):

    self.assertEqual(
AssertionError: '    [36 chars]   
         -    2    + 43    +  49\n
-----    ------    ----    -----\n'
 != '    [36 chars]
         -    2    + 43    +  49\n
-----    ------    ----    -----'

Note the difference in newlines.

I hate to ask but, Do you know any solutions to the problem or do I need to re-write the whole code?
The main problem is if you write [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”], True
The values in the bottom don’t seem to be alined with the ones at the top

You are supposed to check if there are more than 4 problems and your if statement checks if there are more than 5.

Wait but in the rules they say

  • Situations that will return an error:
    • If there are too many problems supplied to the function. The limit is five, anything more will return:
      `Error: Too many problems.

Sorry! I just read the comment that said “more than 4”, I forgot what the actual rules were.

Haha, My code is pretty hard to understand. Might need improve there
No worrys

No, you just need to fix the newlines. If you look at the error, you’ll see that yours (the first one) is different than what is expected (the second one).

I don’t really know how that’s the problem. I’m not so good with that I’m only good with algorithm and maths

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.