Arithmetic Formatter: Operator Terror

Hi . I’ve been struggling at what totally seems to be the Operator problem, while being also flagged for arrangements and solutions. Can someone explain where/how to fix it ?
Or what I should study more about? My browser looks so stressed out.
Thank you! I appreciate it!

My code so far

def arithmetic_arranger(problems, show_answer = False):
  if len(problems)>5:
    return "Error: Too many problems."
  else:
    fLine = ""
    sLine = ""
    thLine = ""
    ansLine = ""
      
    for i, problem in enumerate(problems):
      x=problem.split()
      num1 =x[0]
      num2 =x[2]
      op = x[1]

      if op !="+" and op !="-":
        return "Error: Operator musr be '+' or '-'." 
      else:    
        for digit in num1:
          if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
            return "Error: Numbers must only contain digits."
          else:
            continue
        for digit in num2:
          if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
            return "Error: Numbers must only contain digits."
          else:
            continue

        if len(num1)>4 or len(num2)>4:
          return "Error: Numbers cannot be more than four digits." 
        elif op =="+" or op =="-":                                         
          width =max(len(num1), len(num2))+2
          first = str(num1)
          fLine += first.rjust(width)
          sLine += str(op +" "+ num2.rjust(width-2))
          thLine += str("-"*width)
          
          arr_problems =[]          
          problem is problem in arr_problems      

          if i<len(problems)-1:
            fLine +="    "
            sLine +="    "
            thLine +="    "
            ansLine +="    "
          problem = (fLine+'\n'+sLine+'\n'+thLine)

          return " ".join(str(problem) for problem in arr_problems)
          

          if show_answer== True:
            if op == "+":
              answer1 = int(num1)+int(num2)
              ansLine += str(answer1).rjust(width)
            else:
              answer2 = int(num1)-int(num2)  
              ansLine += str(answer2).rjust(width)
            

            problem = (fLine+'\n'+sLine+'\n'+thLine+'\n'+ansLine)     
            return " ".join(str(problem) for problem in arr_problems)

My browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

1 Like

here you have a typo, and that’s a reason for which you are failing that

can you share your repl? it’s easier to debug with that

2 Likes

Bingo!
Nevertheless, I am still not getting arrangements and solutions right.
So far, you made my day!
Thank you so much!

1 Like

Welcome to the forums and ditto on the repl.it. I ran your tests anyway, and other than the misspelling here is the result:

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 11, 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  [55 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"]

There’s no output from your function here; that’s what the '' != bit means in the AssertionError. I double checked it outside the test as well.

======================================================================

FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 41, 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  [89 chars] 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 arithemetic problems and a second argument of `True`.

Same here.

This is part of the problem. This is where your function stops. Your code can’t get past this return and never executes the rest, so the answers will never show. Worse, this return is in the first iteration of your outer loop, so it’s not getting through all the problems either.

2 Likes

Oh, that’s what was going on!
Thank you so much for your time and very helpful tip!
:star_struck:

I really appreciate th earlier tip from you. Thank you!
But I still can’t get rid of the ‘+’ signs before each Line. So, I’d appreciate more tip.

New code:

          if i<len(problems)-1:
            fLine +="    "
            sLine +="    "
            thLine +="    "
            ansLine +="    "
            problem = (fLine+'\n'+sLine+'\n'+thLine)
            
          if show_answer== True:
            if op == "+":
              answer1 = int(num1)+int(num2)
              ansLine += str(answer1).rjust(width)
            else:
              answer2 = int(num1)-int(num2)  
              ansLine += str(answer2).rjust(width)            
              problem = (fLine+'\n'+sLine+'\n'+thLine+'\n'+ansLine)  
              
          arr = " ".join(str(problem) for problem in arr_problems)

  return arr
Then I get this:

```
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/LightcoralMediumblueTechnicians/test_module.py", line 11, in test_arrangement
    self.assertEqual(
AssertionError: '' != '    3      3801      45      123\n+ 855  [55 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_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/LightcoralMediumblueTechnicians/test_module.py", line 66, in test_solutions
    self.assertEqual(
AssertionError: '' != '   32         1      45      123\n- 698  [89 chars] 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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (failures=2)
```

Thanks both ieahleen and jeremy.a.gray!

do you mean this? the lines starting with + are what you should return, instead your output is still ""

can you please share your repl link?

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

String formatting in this and the budget app are the hardest parts of these challenges. You may need to add rstrip() to the last line, see my code for an example.

1 Like

Sorry for adding to the trouble. I should learn to have the proper manners here.
I posted a reply using the (`) just now. I didn’t use (</>) though.
Thank you for all your help!

Your code is brilliant.
Thank you so super much for sharing! :star_struck:

you still return just an empty string, the value of arr is never changed

1 Like

After investing so many days just to make arr_problems as a list of quatro-deck strings work somehow, I am at peace with rethinking that. I haven’t realized that it was possible to use it as a string which is also a variable that were sliced, until I saw the code built with comments by craig.lunney. Plus, I haven’t known to use the ‘isnumeric()’, either.

So this is totally a Hello Higher World! level of project for me. I must admit that all my suffering was well-deserved and good for me.

I appreciate all the help you have extended to me. You are all truly magnificent!!
Thank you!!!