Arithmetic Code Help

Hello, I’ve been trying to do this project for a couple days now. And I feel like I’m close ( I think).

The project in question has appeared on here before. An arithmetic generator so it looks like the following:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Haven’t been learning coding for long, so any help well be appreciated :slight_smile:

def arithmetic_arranger(problems, show):

    #breaking list up in to several
    top = list()
    top_string= ""
    bottom=list()
    bottom_string=""
    op = list()
    op_string=""
    dash = ""

    # Too many problems error
    if len(problems)>5:
        print(' Error: Too many problems.')

    #separate lists and in to top/operator/bottom
    for lists in problems:
        new = lists.split()
        top.append(new[0])
        op.append(new[1])
        bottom.append(new[2])


    top_string.join(top)
    bottom_string.join(bottom)
    op_string.join(op)

#Caculate and show values. And integer error.
    if type(new[0]) != int or type(new[2]) != int:
        if show==True and op=="-":
            value= int(new[0])-int(new[2])
        else:
            value= int(new[0])+int(new[2])
    else:
        print("Error: Numbers must only contain digits.")

# Operator error
    if op == "*" or op=="/" in op:
        print("Error: Operator must be '+' or '-'.")


# printing dashes
    for i in range(len(top_string.rjust(5))):
        dash += "-"

# return arranged_problems
    if show == True:
        arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(4) + bottom_string.rjust(5) + "\n" + dash + "\n" + str(value)
    else:
        arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(1) + bottom_string.rjust(4) + "\n"+ dash

    return (arranged_problems)

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)

What’s your question? With what exactly do you need help?

what’s the output you get from the tests?

one issue I can imagine is that your function is not returning the errors, instead you just print them

@Sanity:

I can’t seem to get the code to work to get the end result.

The question is:

`### Assignment

Students in primary school often arrange arithmetic problems vertically to make them easier to solve. For example, “235 + 52” becomes:

  235
+  52
-----

Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side. The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.

For example

Function Call:

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----`

@ieahleen: On repl.it I get the following output:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
TypeError: arithmetic_arranger() missing 1 required positional argument: 'show'
exit status 1

On VS Code however, I got nothing.

the function is often called with one single argument, giving an error
your second parameter needs to be optional

so I deleted the show argument from the function. However, now I get the following error.

 python main.py
     
     
-----
FFFEF Error: Too many problems.
F
======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 63, in test_solutions
    actual = arithmetic_arranger(
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 11, in test_arrangement
    self.assertEqual(
AssertionError: '     \n     \n-----' != '    3      3801      45      123\n+ 855    - [51 chars]----'
-      
-      
- -----+     3      3801      45      123
+ + 855    -    2    + 43    +  49
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 39, in test_incorrect_operator
    self.assertEqual(
AssertionError: '     \n     \n-----' != "Error: Operator must be '+' or '-'."
+ Error: Operator must be '+' or '-'.-      
-      
- ----- : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 57, in test_only_digits
    self.assertEqual(
AssertionError: '     \n     \n-----' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.-      
-      
- ----- : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 48, in test_too_many_digits
    self.assertEqual(
AssertionError: '     \n     \n-----' != 'Error: Numbers cannot be more than four digits.'
+ Error: Numbers cannot be more than four digits.-      
-      
- ----- : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 30, in test_too_many_problems
    self.assertEqual(
AssertionError: '     \n     \n-----' != 'Error: Too many problems.'
+ Error: Too many problems.-      
-      
- ----- : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.024s

FAILED (failures=5, errors=1)

as I said, the second argument needs to be optional

now when the function is called as arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"], True) you get that error because you have only one function parameter

for the other cases your output is always

'     \n     \n-----'

so it does not match the requirements

I think I understood, and I wasn’t clear enough before. I removed the show argument from the function call. But it still gives the same error.

def arithmetic_arranger(problems):

    #breaking list up in to several
  top = list()
  top_string= ""
  bottom=list()
  bottom_string=""
  op = list()
  op_string=""
  dash = ""

    # Too many problems error
  if len(problems)>5:
      print(' Error: Too many problems.')

    #separate lists and in to top/operator/bottom
  for lists in problems:
      new = lists.split()
      top.append(new[0])
      op.append(new[1])
      bottom.append(new[2])


  top_string.join(top)
  bottom_string.join(bottom)
  op_string.join(op)

#Caculate and show values. And integer error.
  if type(new[0]) != int or type(new[2]) != int:
      if op=="-":
        value= int(new[0])-int(new[2])
      else:
        value= int(new[0])+int(new[2])
  else:
      print("Error: Numbers must only contain digits.")

# Operator error
  if (op == "*" or op=="/") in op:
      print("Error: Operator must be '+' or '-'.")


# printing dashes
  for i in range(len(top_string.rjust(5))):
      dash += "-"

# return arranged_problems
  arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(1) + bottom_string.rjust(4) + "\n"+ dash

  return arranged_problems

it needs to be optional because the tests sometimes call it with a second argument

there is no place to put it in the argument list, and you get an error

you have written it to take one positional argument

but sometimes the tests use two positional arguments

so you need to make both situation possible, making the second argument optional

So I realised how much it all sucked. I deleted it and started again since.
In Vscode it works perfectly fine and outputs the required values in the right format. However, on repl.it it keeps coming up with failures, and I don’t know what mistake I have made - I have tried to debug myself numerous times.

My code:

def arithmetic_arranger(problems, show=False):

    # Too many problems error
    if len(problems)>4:
        print('Error: Too many problems.')
    
    else:

    #separate lists and in to top/operator/bottom
        one = ""
        two=""
        three=""
        test=list()
        top=list()
        top1=""
        final_values=list()
        dashes=list()

        for lists in problems:
            new = lists.split()
            one = new[0]
            two = new[1]
            three = new[2]
    
    #Caculate and show values. And integer error.
            if one.isnumeric()==False or three.isnumeric()==False:
                return ("Error: Numbers must only contain digits.")
                
            elif len(one) >4 or len(three)>4:
                return ("Error: Numbers cannot be more than four digits.")
# Operator error
            elif "/" in two or "*" in two:
                return ("Error: Operator must be '+' or '-'.")
            else:
                if two=="-":
                    value= int(one)-int(three)
                else:
                    value= int(one)+int(three)
            #width adjustment
            if len(one)>=len(three):
                width = len(one)
            else: 
                width=len(three)
            #Append numbers to top and bottom lists
            top.append(one.rjust(width+2))
            two_three = two.rjust(1) + three.rjust(width+1)
            test.append(two_three)
            final_values.append(str(value).rjust(width+2))
            #Adding dashed lines
            if len(one)>=len(three):
                dash = (len(one)+2)*"-"
                dashes.append((dash).rjust(width))
            else:
                dash = len(two_three)*"-"
                dashes.append((dash).rjust(width))

        #convert appended lists to strings
        top_string=" ".join(str(x) for x in top)
        middle_string=" ".join(str(x) for x in test)
        final_value_string=" ".join(str(x) for x in final_values)
        dashes_string=" ".join(str(x) for x in dashes)
        # return arranged_problems
        if show == True:
            print(top_string+"\n"+ middle_string+"\n"+dashes_string+"\n"+final_value_string)
        else: 
            print(top_string+"\n"+ middle_string+"\n"+dashes_string)

            return 

The error messages I get on repl.it:

python main.py
   32   3801   45   123
+ 698 -    2 + 43 +  49
----- ------ ---- -----
None
    3   3801   45   123
+ 855 -    2 + 43 +  49
----- ------ ---- -----
F..   32      1   45   123
- 698 - 3801 + 43 +  49
----- ------ ---- -----
 -666  -3800   88   172
F.Error: Too many problems.
F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-7/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: None != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-7/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 arithemetic problems and a second argument of `True`.')
AssertionError: None != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-7/test_module.py", line 19, in test_too_many_problems
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: None != 'Error: Too many problems.' : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.005s

FAILED (failures=3)
 

I think it’s something to do with additional spaces being added somewhere, but I’m unsure how or why.

Thank you!

Function isn’t supposed to print anything on it own, but return the appropriate string.

1 Like

Ah yeah. I’ve amended it and it’s knocked my failures down to two. Now I get the following:

def arithmetic_arranger(problems, show=False):

    # Too many problems error
    if len(problems)>4:
        return('Error: Too many problems.')
    
    else:

    #separate lists and in to top/operator/bottom
        one = ""
        two=""
        three=""
        test=list()
        top=list()
        top1=""
        final_values=list()
        dashes=list()

        for lists in problems:
            new = lists.split()
            one = new[0]
            two = new[1]
            three = new[2]
    
    #Caculate and show values. And integer error.
            if one.isnumeric()==False or three.isnumeric()==False:
                return ("Error: Numbers must only contain digits.")
                
            elif len(one) >4 or len(three)>4:
                return ("Error: Numbers cannot be more than four digits.")
# Operator error
            elif "/" in two or "*" in two:
                return ("Error: Operator must be '+' or '-'.")
            else:
                if two=="-":
                    value= int(one)-int(three)
                else:
                    value= int(one)+int(three)
            #width adjustment
            if len(one)>=len(three):
                width = len(one)
            else: 
                width=len(three)
            #Append numbers to top and bottom lists
            top.append(one.rjust(width+2))
            two_three = two.rjust(1) + three.rjust(width+1)
            test.append(two_three)
            final_values.append(str(value).rjust(width+2))
            #Adding dashed lines
            if len(one)>=len(three):
                dash = (len(one)+2)*"-"
                dashes.append((dash).rjust(width))
            else:
                dash = len(two_three)*"-"
                dashes.append((dash).rjust(width))

        #convert appended lists to strings
        top_string=" ".join(str(x) for x in top)
        middle_string=" ".join(str(x) for x in test)
        final_value_string=" ".join(str(x) for x in final_values)
        dashes_string=" ".join(str(x) for x in dashes)
        # return arranged_problems
        if show == True:
            return (top_string+"\n"+ middle_string+"\n"+dashes_string+"\n"+final_value_string)
        else: 
            return (top_string+"\n"+ middle_string+"\n"+dashes_string)

 python main.py
  98   3801   45   123
+ 35 -    2 + 43 +  49
---- ------ ---- -----
 133   3799   88   172
   32   3801   45   123
+ 698 -    2 + 43 +  49
----- ------ ---- -----
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/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: '    3   3801   45   123\n+ 855 -    2 + 43 +  49\[20 chars]----' != '    3      3801      45      123\n+ 855    -    2[47 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 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/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 arithemetic problems and a second argument of `True`.')
AssertionError: '   32      1   45   123\n- 698 - 3801 + 43 +  49\n--[42 chars] 172' != '   32         1      45      123\n- 698    - 3801   [78 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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.060s

FAILED (failures=2)

It looks like you do not have enough space between problems side to side.
From the README:

There should be four spaces between each problem.

Thanks for pointing that out. I made the adjustments to account for that, again, on Vscode it comes out fine, but on Repl.it I get two failures.

 python main.py
      32      3801      45      123
   + 698    -    2    + 43    +  49
--------    ------    ----    -----
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/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: '       3      3801      45      123\n   + 855[60 chars]----' != '    3      3801      45      123\n+ 855    - [51 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 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/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 arithemetic problems and a second argument of `True`.')
AssertionError: '      32         1      45      123\n   - 69[98 chars] 172' != '   32         1      45      123\n- 698    -[86 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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.008s

FAILED (failures=2)

you have too many spaces now at the beginning

I think I solved it, finally. Thanks for the help all of you, I really do appreciate it!

1 Like

How did you deal with the require more than one position arguments error? I tried adding *args to my definition but isn’t helping.

you need to have a second optional parameter in your function