Arithmetic_formatter: white_spaces

Hi! I am having a formatting problem with my arithmetic_formatter function. I have passed 4 out of 6 tests so far. The problem is that when i print out the calculations i appear to have a single whitespace at the end of each line of strings (s1, s2, s3 and s4 - which represents the result). I don’t know how to get rid of them. Does anybody have any idea how? Been buggin be for some time and i would appreciate very much some help :sweat_smile:

Down here is my code. I tested it in vscode and it seemed to work fine, no whitespaces at the end of the outputs.
Here is the picture with the errors from replit:

def arithmetic_arranger(problems, *sol):
    if len(problems) > 5:
        return "Error: Too many problems."
    list_p = []
    for elem in problems:
        list_p.append(elem)
    s1 = ' '
    s2 = ' '
    s3 = ' '
    s4 = ' '
    for i in range(len(list_p)):
        s = list_p[i].split()
        if s[1] != "+" and s[1] != "-":
            return "Error: Operator must be '+' or '-'."
        for num in s[0]:
            if not num.isnumeric():
                return "Error: Numbers must only contain digits."
        for num in s[2]:
            if not num.isnumeric():
                return "Error: Numbers must only contain digits."
        if len(s[0]) > 4 or len(s[2]) > 4:
                return "Error: Numbers cannot be more than four digits."
        if s[1] == "+":
            rez = int(s[0]) + int(s[2])
            rez = str(rez)
        else:
            rez = str(int(s[0]) - int(s[2]))
        if len(s[0]) <= len(s[2]):
            s1 = s1 + f"  {' ' * abs(len(s[2]) - len(s[0]))}{s[0]}    "
            s2 = s2 + f"{s[1]} {s[2]}    "
            s3 = s3 + f"{'-' * (2 + len(s[2]))}    "
            s4 = s4 + f"{' ' * (2 + len(s[2]) - len(rez))}{rez}    "
        else:
            s1 = s1 + f"  {s[0]}    "
            s2 = s2 + f"{s[1]} {' ' * abs(len(s[0]) - len(s[2]))}{s[2]}    "
            s3 = s3 + f"{'-' * (2 + len(s[0]))}    "
            s4 = s4 + f"{' ' * (2 + len(s[0]) - len(rez))}{rez}    "
    s1 = s1.rstrip()
    s2 = s2.rstrip()
    s3 = s3.rstrip()
    s4 = s4.rstrip()
    if sol:
        arranged_problems = s1 + "\n"+ s2 + "\n"+ s3 + "\n"+ s4
    else:
        arranged_problems = s1 + "\n"+ s2 + "\n"+ s3
    return arranged_problems

Here’s your output on top and the expected on bottom:

    32         1      45      123
 - 698    - 3801    + 43    +  49
 -----    ------    ----    -----
  -666     -3800      88      172
==================================
   32         1      45      123
- 698    - 3801    + 43    +  49
-----    ------    ----    -----
 -666     -3800      88      172

Looks like you have an extra space at the beginning of each line. I assume this is the offending code:

I believe you meant the empty string, i.e. s1 = ''.

2 Likes

Yes, that fixed it. Thank you very much for the help! :smiley:

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