I need help on Aritmatic formatter

How do I print my output in one line?

def arithmetic_arranger(problems, args = True):
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        arranged_problems = []
        for problem in problems:
            x =problem.split()
            num1 = x[0]
            num2 = x[2]
            opr = x[1]
            if opr not in ["+" , "-"]:
                return "Error: Operator must be '+' or '-'."
            else:
                for y in num1:
                    if y.isdigit():
                        continue
                    else:
                        return "Error: Numbers must only contain digits."
                for z in num2:
                    if z.isdigit():
                        continue
                    else:
                        return "Error: Numbers must only contain digits."
                
                if len(num1) > 4 or len(num2) > 4:
                    return "Error: Numbers cannot be more than four digits."
                else:
                    
                    if opr == "+":
                        answer = int(num1) + int(num2)
                    elif opr == "-":
                        answer = int(num1) - int(num2)
                    
                    width = max(len(num1), len(num2)) + 2
                    fline = str(num1.rjust(width))
                    sline = str(opr + num2.rjust(width - 1))
                    tline = str("-" * width)
                    aline = str(answer).rjust(width)
                     
                    if args == True:
                        arranged_problem = (fline + "\n" + sline + "\n" + tline + "\n" + aline)
                        arranged_problems.append(arranged_problem)
                    elif args == False:
                        arranged_problem = (fline + "\n" + sline + "\n" + tline + "\n")
                        arranged_problems.append(arranged_problem)
        for kk in arranged_problems:
            print("\n")
            print(kk)
    return arranged_problems
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))

do not use print
to pass the tests you need to have the value returned from the function be a string of required value

I tried this but the output is looks like this
[' 32\n+ 698\n-----\n 730', ' 3801\n- 2\n------\n 3799', ' 45\n+ 43\n----\n 88', ' 123\n+ 49\n-----\n 172']

that seems a list of strings, not a string

plus the operations needs to be side by side

(all the first operands on first line, all second operands on second line…)

Now, Im only get last problem that we enter.(123 + 49)

you probably need to review a bit your logic

rememer that to get something like

A      D
B      E
C      F

you need to write

"A      D\nB      E\nC      F"

I couldn’t found the error.I failed the first test sitiation.My code is:

def arithmetic_arranger(problems, args = True):
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        fline = ""
        sline = ""
        tline = ""
        aline = ""
        arranged_problems = []
        for problem in problems:
            x = problem.split()
            num1 = x[0]
            num2 = x[2]
            opr = x[1]
            if opr not in ["+" , "-"]:
                return "Error: Operator must be '+' or '-'."
            else:
                for y in num1:
                    if y.isdigit():
                        continue
                    else:
                        return "Error: Numbers must only contain digits."
                for z in num2:
                    if z.isdigit():
                        continue
                    else:
                        return "Error: Numbers must only contain digits."
                
                if len(num1) > 4 or len(num2) > 4:
                    return "Error: Numbers cannot be more than four digits."
                else:
                    
                    if opr == "+":
                        answer = int(num1) + int(num2)
                    elif opr == "-":
                        answer = int(num1) - int(num2)
                    
                    width = max(len(num1), len(num2)) + 2
                    fline += str(num1.rjust(width))
                    sline += str(opr + num2.rjust(width - 1))
                    tline += str("-" * width)
                    aline += str(answer).rjust(width)

                    if problem != problems[-1]:
                        fline += "    "
                        sline += "    "
                        tline += "    "
                        aline += "    " 
                        
                    if args == True:
                        arranged_problems = (fline + "\n" + sline + "\n" + tline + "\n" + aline)
                        
                    elif args == False:
                        arranged_problems = (fline + "\n" + sline + "\n" + tline + "\n")
                        
    return arranged_problems

Error :

FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/RingedMellowDecimal/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: '    [68 chars]-    ------    ----    -----\n  858      3799      88      172' != '    [68 chars]-    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----
?                                 -
+ -----    ------    ----    ------   858      3799      88      172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

look at the line starting with a ?, it marks the difference betweek what you should have and what you have, look at that dash: there is an extra character at the end of your string that shouldn’t be there

1 Like

Thank you so much for helping me.