Python arithmetic formatter

val = ["98 + 35", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_arranger(values,state = False):
    #split list
    
    new_lst = [x.strip().split() for x in values]
    first = []
    second = []
    not_first = []
    its_width = []
    second_width = []
    dashes =[]
    
    
    #  #appending the items into their corresponding lists
    for i in new_lst:
        first.append(i[0])
        second.append(i[2])
        not_first.append(i[1:])
    # this will be the width of the digits in the second line
    for i in new_lst:
        new_width = len(max(i[0],i[2])) + 1
        second_width.append(new_width)
    #this will be the width of items in the first line
    for i in new_lst:
        width = len(max(i[0],i[2])) + 2
        its_width.append(width)
        return its_width
        return second_width
print(arithmetic_arranger(val))

Hey can you help me with the above code this is my first project and I am stuck because the last values of the its_width and second_width are not correct . I don’t know what I did wrong but this is the part of my code that im failing to understand why.

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

Thank you.

It is related to the arithmetic arranger project but this is a code snippet extracted from my whole code. I really need to know why.
Also I am new to this forum and I dont know where the ask for help button is.

What values do you expect its_width and second_width to have?

its_width = [4, 6, 4, 4]
second_width = [3, 5, 3, 3]
i expect it to be:
its_width = [4,6,4,5]
second_width = [3,5,3,4]

Let me just post my whole code here for further clarifications

val = ["98 + 35", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_arranger(values,state = False):
    #split list
    
    new_lst = [x.strip().split() for x in values]
    first = []
    second = []
    not_first = []
    its_width = []
    second_width = []
    dashes =[]
    #checking if given digit does not exceed 4  
    for item in new_lst:
        for j in item:
            if len(j)>4:
                return('Error: Numbers cannot be more than four digits.')
        # checking if the values passed on do not contain non-integers
        if not (item[0].isdigit() and item[2].isdigit()):
            return('Error: Numbers must only contain digits.')
    #to return an error when the values exceed 5
    if len(values) >5 :
        return( 'Error: Too many problems.')
    for inp in new_lst:
        if inp[1] != "+" and inp[1] != "-":
            return("Error: Operator must be '+' or '-'.")
    #  #appending the items into their corresponding lists
    for i in new_lst:
        first.append(i[0])
        second.append(i[2])
        not_first.append(i[1:])
    # this will be the width of the digits in the second line
        new_width = len(max(i[0],i[2])) + 1
        second_width.append(new_width)
    #this will be the width of items in the first line
        width = len(max(i[0],i[2])) + 2
        its_width.append(width)
    for i in first:
        # using try to avoid errors when fewer items in the list are passed on
        try:
            if i == first[0]:
                first[0]=i.rjust(its_width[0])
            elif i == first[1]:
                first[1]=i.rjust(its_width[1])
            elif i == first[2]:
                first[2]=i.rjust(its_width[2])
            elif i == first[3]:
                first[3]=i.rjust(its_width[3])
            elif i == first[4]:
                first[4] = i.rjust(its_width[4])
        except IndexError:
            pass
    first_line = '    '.join(i for i in first)
    for i in not_first:
        try:
            if i[1] == not_first[0][1]:
                not_first[0][1]=i[1].rjust(second_width[0])
            elif i[1] == not_first[1][1]:
                not_first[1][1]=i[1].rjust(second_width[1])
            elif i[1] == not_first[2][1]:
                not_first[2][1]=i[1].rjust(second_width[2])
            elif i[1] == not_first[3][1]:
                not_first[3][1]=i[1].rjust(second_width[3])
            elif i[1] == not_first[4][1]:
                not_first[4][1] = i[1].rjust(second_width[4])
        except IndexError:
            pass
    joined = list(map(''.join, not_first))
    second_line = '    '.join(i for i in joined)
    
    for item in range(len(new_lst)):
        try:
            if item ==0:
                dashes.append(('-'*its_width[0]))
            elif item ==1:
                dashes.append(('-'*its_width[1]))
            elif item ==2:
                dashes.append(('-'*its_width[2]))
            elif item ==3:
                dashes.append(('-'*its_width[3]))
            elif item ==4:
                dashes.append(('-'*its_width[4]))
            
        except IndexError:
            pass
        joined_dashes = '    '.join(i for i in dashes)
    #checking the state to print out the answers 
    if state == True:
        calc= []
        for item in new_lst:
            if item[1] == '+':
                calc.append(int(item[0])+int(item[2]))
            elif item[1] == '-':
                calc.append(int(item[0])-int(item[2]))
        for b in calc:
            calc = list(map(str,calc))
        for b in calc:
            try:
                if b == calc[0]:
                    calc[0] = b.rjust(its_width[0])
                elif b == calc[1]:
                    calc[1] = b.rjust(its_width[1])
                elif b == calc[2]:
                    calc[2] = b.rjust(its_width[2])
                elif b == calc[3]:
                    calc[3] = b.rjust(its_width[3])
                elif b == calc[4]:
                    calc[4] = b.rjust(its_width[4])        
            except IndexError:
                pass
            last_line = '    '.join(i for i in calc)
        return first_line+'\n'+second_line+'\n'+joined_dashes+'\n'+last_line
    else:        
        return first_line+'\n'+second_line+'\n'+joined_dashes  
print(arithmetic_arranger(val))

when i ran tests on it i get this error because the last values of its_width and second_width are not correct.

======================================================================
FAIL: test_arrangement (__main__.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:/Users/Stella/Desktop/python projects/arithmetic arranger/boilerplate-arithmetic-formatter/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [19 chars]     123\n+ 855    -    2    + 43    + 49\n---[24 chars]----' != '    [19 chars]      123\n+ 855    -    2    + 43
   +  49\n-[27 chars]----'
-     3      3801      45     123
+     3      3801      45      123
?                        +
- + 855    -    2    + 43    + 49
+ + 855    -    2    + 43    +  49
?                             +
- -----    ------    ----    ----+ -----    ------    ----    -----?                                +
 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (__main__.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:/Users/Stella/Desktop/python projects/arithmetic arranger/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: '   3[19 chars]     123\n- 698    - 3801    + 43    + 49\n---[57 chars] 172' != '   3[19 chars]      123\n- 698    - 3801    + 43
   +  49\n-[61 chars] 172'
-    32         1      45     123
+    32         1      45      123
?                        +
- - 698    - 3801    + 43    + 49
+ - 698    - 3801    + 43    +  49
?                             +
- -----    ------    ----    ----
+ -----    ------    ----    -----
?                            +
-  -666     -3800      88     172+  -666     -3800      88      172?                        +
 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.081s

FAILED (failures=2)

Take a closer look at the way function is determining these values. Go through it step-by-step - what are i[0] and i[2], what max function return for them and what later len function returns, see if all of these numbers are what is expected.

the lines with + are the expected output, those with - is yours, and the ? shows the difference, it seems you are missing some spaces

len(max((‘123’ , ‘49’))) + 2 != 4 it is 5 but it keeps on returning 4
and len(max((‘123’ , ‘49’))) +1 != 3 it is 4 but it keeps on returning 3
thats what affects the last values of the output and the 2 tests fail here. I dont know why.

Small hint regarding this - check what is returned by max('2222222', '1') and max('aaaaaaa', 'b')

I am aware thats why I added len(max(…)) to convert it into integers in my code

Did you try it and did it give the result you expected?

Thanks the second example helped me understand my problem