Arithmetic Formatter fails at test_arrangement and test_solutions

Tell us what’s happening:
Hello,
it seems the output matches the answer, but still get fails?

Your code so far

import re

def arithmetic_arranger(*problems):

  #first to judge if the problem can be continued
  if len(problems[0]) > 5:
    return "Error: Too many problems."
  for strs in problems[0]:
    if re.search("[*/]", strs):
      return "Error: Operator must be '+' or '-'."
    elif re.search("[a-zA-Z]", strs):
      return "Error: Numbers must only contain digits."
    else:
      for i in strs.split():
        if len(i)>4:
          return "Error: Numbers cannot be more than four digits."
  
  #second start to process the str array for further use
  #print(cal)
  cal=[]
  for item in problems[0]:
    cal1=item.split()
    if cal1[1]=='+':
      cal1.append(int(cal1[0])+int(cal1[2]))
    else:
      cal1.append(int(cal1[0])-int(cal1[2]))
    cal1.append(max(len(cal1[0]),len(cal1[2]),len(str(cal1[3]))+2))
    cal.append(cal1)
    

  #third to print the array
  #generate print format expression
  fmt=''
  for j in range(0,len(cal)):#len(cal) columns of equations to print
    fmt+=('{:>'+str(cal[j][4])+'}'+'    ') 
  fmt=fmt.strip()
    
  answer=''

  if len(cal)==4:
    answer=((fmt).format(cal[0][0],cal[1][0],cal[2][0],cal[3][0]))+'\n'+\
            ((fmt).format(cal[0][1]+' '*(cal[0][4]-len(cal[0][2])-1)+cal[0][2],
                        cal[1][1]+' '*(cal[1][4]-len(cal[1][2])-1)+cal[1][2],
                        cal[2][1]+' '*(cal[2][4]-len(cal[2][2])-1)+cal[2][2],
                        cal[3][1]+' '*(cal[3][4]-len(cal[3][2])-1)+cal[3][2]))+'\n'+\
            ((fmt).format('-'*cal[0][4],'-'*cal[1][4],'-'*cal[2][4],'-'*cal[3][4]))

  elif len(cal)==5:
    answer=((fmt).format(cal[0][0],cal[1][0],cal[2][0],cal[3][0],cal[4][0]))+'\n'+\
        ((fmt).format(cal[0][1]+' '*(cal[0][4]-len(cal[0][2])-1)+cal[0][2],
                      cal[1][1]+' '*(cal[1][4]-len(cal[1][2])-1)+cal[1][2],
                      cal[2][1]+' '*(cal[2][4]-len(cal[2][2])-1)+cal[2][2],
                      cal[3][1]+' '*(cal[3][4]-len(cal[3][2])-1)+cal[3][2],
                      cal[4][1]+' '*(cal[4][4]-len(cal[4][2])-1)+cal[4][2]))+'\n'+\
        ((fmt).format('-'*cal[0][4],'-'*cal[1][4],'-'*cal[2][4],'-'*cal[3][4],'-'*cal[4][4]))

  if len(problems)>1:
    if len(cal)==4:
      answer=answer+'\n'+((fmt).format(cal[0][3],cal[1][3],cal[2][3],cal[3][3]))

    if len(cal)==5:
      answer=answer+'\n'+((fmt).format(cal[0][3],cal[1][3],cal[2][3],cal[3][3],cal[4][3]))
  
  print('answer,length of answer',len(answer))
  print(answer)
  return answer

The test file is:

class UnitTests(unittest.TestCase):
    def test_arrangement(self):
        actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
        expected = "    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----"
        print('test,length of test:',len("    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----"))
        print("    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----")
'''
etc.
'''

  def test_solutions(self):
        actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
        expected = "   32         1      45      123\n- 698    - 3801    + 43    +  49\n-----    ------    ----    -----\n -666     -3800      88      172"
        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`.')
        print('test,length of test:',len("   32         1      45      123\n- 698    - 3801    + 43    +  49\n-----    ------    ----    -----\n -666     -3800      88      172"))
        print("   32         1      45      123\n- 698    - 3801    + 43    +  49\n-----    ------    ----    -----\n -666     -3800      88      172")


The result is:

answer,length of answer 98
    3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
test,length of test: 98
    3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
answer,length of answer 122
  11     3801      1      123          1
+  4    -2999    + 2    +  49    -  9380
----    -----    ---    -----    -------
F..answer,length of answer 139
    32          1      45      123
-  698    -  3801    + 43    +  49
------    -------    ----    -----
  -666      -3800      88      172
F..

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

The Fs are probably test suite failures.

I don’t see a return statement anywhere in your code, which is probably causing you trouble. You need to return a string holding the output, not print the output to the terminal.


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 (’).

thank you. first time poster. i was wondering how to format the post.

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