Arithmetic Formatter Help End

Tell us what’s happening:
Describe your issue in detail here.
Hi everyone, so I’ve been trying to finish the arithmetic formatter and I’ve reached the end of my code. However, I have 1 failure with 5 errors and I can’t seem to find where the errors are. Also as you can see the output for the first list given is not fully printed as I have probably done the tabulation for arranged_problems where it is not suppose to be done.

Thanks a lot in advance.

Your code so far

def arithmetic_arranger(problems, see):

  """ Declaration of list, variables and strings used in program """
  first_operand = list()
  operator = list()
  second_operand = list()
  max_length = list()
  dashes = list()
  operation = list()
  upper_part = ""
  lower_part = ""
  dashesline = ""
  results = ""
  index = 0

  # Checking if the length of the list is above 5
  if len(problems) > 5:
    print(" Error: Too many problems")
  
  # Divide the list into sub lists
  for increment in problems:
    new_item = increment.split()
    
    # Divide the list into sub list
    first_operand.append(new_item[0])
    operator.append(new_item[1])
    second_operand.append(new_item[2])

  """ Checking if the operands are only digits (must be separated into two different for loops if we do not want to see the error written 4 times) """
  for go_over in first_operand:
    if not go_over.isnumeric():
      print("Error: Numbers must only contain digits")
  for go_over in second_operand:
    if not go_over.isnumeric():
      print("Error: Numbers must only contain digits")


  """ Checking if numbers are not above 4 digits (must be separated into two different for loops if we do not want to see the error written 4 times) """
  for go_over in first_operand:
    if len(go_over) > 4:
      print("Error: Numbers cannot be more than four digits")
  for go_over in second_operand:
    if len(go_over) > 4:
      print("Error: Numbers cannot be more than four digits")

  # Checking if the operator is a '+' or a '-'
  for go_over in operator:
    index += 1
    if go_over == "*" or go_over == "/":
      print("Error: Operator must be '+' or '-'")
    # Doing the calculation for '+' and '-'
    elif go_over == "+":
      operation.append(str(int(first_operand[index-1]) + int(second_operand[index-1])))
    elif go_over == "-":
      operation.append(str(int(first_operand[index-1]) - int(second_operand[index-1])))
    
    """ Having the maximum length of each calculation # and the numbers of dashes needed for each of those calculations """
    for go_over in range(len(first_operand)):
      max_length.append(max(len(first_operand[go_over]),len(second_operand[go_over])))
      dashes.append("-"*(2+max_length[go_over]))
    
  for go_over in range(len(first_operand)):
    if see == True:
      if go_over == 0:
        upper_part += first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
        dashesline += dashes[go_over]
        results += operation[go_over].rjust(2+max_length[go_over])
      else:
        upper_part += "    " + first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += "    " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])
        dashesline += "    " + dashes[go_over]
        results += "    " + operation[go_over].rjust(2+max_length[go_over])
      
      arranged_problems = upper_part + "\n" + lower_part + "\n" + dashesline + "\n" + results

    else:
      if go_over == 0:
        upper_part += first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
      else:
        upper_part += "    " + first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += "    " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])

      arranged_problems = upper_part + "\n" + lower_part
    
    return arranged_problems

Here is the response that I get from the console

python main.py
   32
+ 698
-----
  730
EEEFEE
======================================================================
ERROR: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 8, in test_arrangement
    actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 22, in test_incorrect_operator
    actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 27, in test_too_many_digits
    actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 17, in test_too_many_problems
    actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/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: '   32\n- 698\n-----\n -666' != '   32         1      45      123\n- 698    - 3[84 chars] 172'
-    32
- - 698
- -----
-  -666+    32         1      45      123
+ - 698    - 3801    + 43    +  49
+ -----    ------    ----    -----
+  -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.002s

FAILED (failures=1, errors=5)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Hi eneko.cubillas,
Do you have an result for us to look at, so we can see where the alignment is off?
I think your operators are missing from the result too…

1 Like

Hi Brain,

Thank you for the quick response, I have added the following print() at the end of my code without the operators just to see how it would result

print(upper_part+ "\n" + lower_part + "\n" + dashelines + "\n" + final_part )

This is the result I have:

32    3801    45    123    
+ 6984    - 2    + 43    + 49
------    ------    ------    ------
7016    3799    88    172

I have not yet integrated the operators which I suppose will disrupt the whole thing further

The hardest part of this challenge is indeed the spacing.
I have added a result from replit, I’d suggest you use replit too, so you can see what errors are created.

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
  1. As you can see, the spacing between each problem is constant (4 spaces between the last digit of a problem and the operator of the next problem, you have this covered.
  2. The width of the dashline (number of dashes) depends on the length of largest/longest operand. Looking at your result, this does not work (for example: at the 3rd problem: 45 + 43 = 88…the dashline should be 4 dashes instead of 6).
    So including the operators is a good idea, leaving them out will not disrupt your alignment, but it does not help either.
  3. Aligning all operands and dashline, so they are neatly stacked is needed (and gives a better view on if and where spacing needs to be adjusted too).

Wthout me rewriting your code, I think if you work on 2 and 3 in replit (so you can let the test module do it’s test-things and see which errors are appearing), you’re there.

1 Like

Cheers for the reply.

I’ll check everything out tomorrow :smiley:

Finally found the way around it

1 Like

Well done, you have put a lot of effort in this :clap:

1 Like

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