Arithmetic formatter HELP NEEDED

Hello! I am having trouble understanding the output and what I am doing wrong in my code.

This is my code:

def arithmetic_arranger(problems, second_argument = False):
  if len(problems) > 5:
    return "Error: Too many problems."

  first_number = ""
  operator = ""
  second_number = ""
  line_two = ""
  divider = ""
  result = ""
  
  #seperating into equations into parts 
  for char in problems:
    char_splitted = char.split()
    float(char_splitted[0])
    float(char_splitted[2])
    if char_splitted[0].isalpha(): 
        return "Error: Numbers must only contain digits."

  if char_splitted[1] not in ["+", "-"]:
    return "Error: Operator must be '+' or '-'."

    #changing float to string to check num length 
    str(char_splitted[0])
    str(char_splitted[2])

  if len(char_splitted[0]) > 4 or len(char_splitted[2]) > 4:
    return "Error: Numbers cannot be more than four digits."
  
  #giving variables to sliced parts
  first_number = char_splitted[0]
  operator = char_splitted[1]
  line_two = operator + char_splitted[2]
  divider = "-" * len(line_two)
  result = str(eval(char))

  #indenting numbers & formatting
  numbers_total = '{:>4} {:>4} {:>4}'.format(first_number, second_number, result)
  final_product = ""
  if second_argument:
    final_product = first_number + '\n' + line_two + '\n' + divider + '\n' + result
      
    return final_product

And this was the output:

None
FFEFF.
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-4/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/boilerplate-arithmetic-formatter-4/arithmetic_arranger.py", line 16, in arithmetic_arranger
    float(char_splitted[2])
ValueError: could not convert string to float: '3g5'

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-4/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_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-4/test_module.py", line 24, in test_incorrect_operator
    self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: None != "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_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-4/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: '123\n+49\n---\n172' != '   32         1      45      123\n- 698  [89 chars] 172'
- 123
- +49
- ---
- 172+    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`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-4/test_module.py", line 29, in test_too_many_digits
    self.assertEqual(actual, expected, '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."')
AssertionError: None != '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."

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=4, errors=1)

This is for the first problem.
Try this actually you were converting the problems to float, before finding out if there is a character present or not. We cannot convert char to float that is why it was giving an error.

1 Like

Thank you for the help!

1 Like

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