Arithmetic Formatter Problem - help

Tell us what’s happening:
I am not sure how to correct some of the errors that are occurring. For example, I am checking for length, operator, and trying ensure the string is a number but I am not able to pass the tests. What is wrong with my code so far? I am pretty new to Python so not sure I fully understand what else I should try to do in order to get some of the code blocks to pass.

I understand why I am getting formatting errors. My plan is to check for Errors first with the input then define arrange_problems so the programs output is the arranged and formatted problem.

Here is a link to my Repl.
Replit

Your code so far

def arithmetic_arranger(problems):
    if len(problems) > 5: 
        return("Error: Too many problems.")
    #splits problems
    for p in problems: 
        prob = p.split(' ')
        num1 = prob[0]
        operand = prob[1]
        num2 = prob[2]
   
    #block to check for length, operator, and digits
        if not len(num1) < 4 or not len(num2) < 4:
            return("Error: Numbers must not be longer than 4 digits.")
            
        if not operand == '+' or not operand == '-':
            return("Error: Operator must be '+' or '-'.")

        if not num1.isnumeric() or not num2.isnumeric():
            return("Error: Numbers must only contain digits.")
        else:
            pass

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

in one case you have the wrong string
it needs to always be the exact same as what is requested in the instructions

- Error: Numbers must not be longer than 4 digits.
?                ^^^^^       ^ ^^ -      ^
+ Error: Numbers cannot be more than four digits.
?                ^^^       ^ ^       ^^^^

the one starting with - is your output, the one with + is the expected, the lines starting with ? show the difference


then there are two cases in which you are returning Error: Operator must be '+' or '-'. when you shouldn’t.

FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FvNWlXbN9YC/test_module.py", line 34, in test_only_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: "Error: Operator must be '+' or '-'." != 'Error: Numbers must only contain digits.'
- Error: Operator must be '+' or '-'.
+ 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_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FvNWlXbN9YC/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: "Error: Operator must be '+' or '-'." != '    3      3801      45      123\n+ 855 [56 chars]----'
- Error: Operator must be '+' or '-'.
+     3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

you need to correct this expression, maybe a cheatsheet on operators precedence may help you, or on how to combine expressions

Got it! Thank you and I appreciate the info. Now to figure out the format.

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