Cant figure out why my arithmetic arranger is failing 6 tests

my arithmetic arranger is failing on all but the verification tests and I haven’t been able to pin point why. Code below.

def arithmetic_arranger(problems, show_answer=False):
  #creating variables for future use
    first_line=''
    second_line=''
    dashes=''
    sumline=''
    #verification
    if len(problems) > 5:
        return 'Error: Too many problems.'
    for item in problems:
        digit_counter = item.split()
        if digit_counter[0].isdigit() == False or digit_counter[2].isdigit() == False:
            return "Error: Numbers must only contain digits."
        if len(digit_counter[0]) > 4 or len(digit_counter[2]) > 4:
            return "Error: Numbers cannot be more than four digits."
        if digit_counter[1] != '+' and digit_counter[1] != '-':
            return "Error: Operator must be '+' or '-'."
      #now assiging variables and getting the length of each problem 
        num_one = digit_counter[0]
        operator = digit_counter[1]
        num_two = digit_counter[2]
        total_length = max(len(num_one), len(num_two)) + 2
        #getting answers and creating the answer string
        if show_answer == True:
            sumline += str(eval(item)).rjust(total_length) + '    '
        #creating the strings 
        first_line += num_one.rjust(total_length)+'    '
        second_line += operator + num_two.rjust(total_length-1)+'    '
        dashes += ''.rjust(total_length,'-')+'    '
    #putting it all together 
    if show_answer == False:
        arranged_problems =f'{first_line}\n{second_line}\n{dashes}'.rstrip()
    else:
        arranged_problems =f'{first_line}\n{second_line}\n{dashes}\n{sumline}'.rstrip()

    print(arranged_problems)
    return arranged_problems

I’ve thought it might be from the .rstrip() at the end of either string not working quite how I imagine but I’m not really sure.

What errors exactly are you getting from tests?

1 Like

The output doesnt match the expected output. I’m getting an ‘AssertionError’ back form the test, i assume the issue has something to do with white space since what im producing and what they expect looks exactly the same its gotta be something that i cant see like that. But I’m really not sure.

Yeah, now go and copy-paste them here so we can see them :wink:
Or just directly post a link to your replit. Either way, without the actual error, it’s pretty damn hard to figure out where a problem might be.

https://replit.com/@LJC6/boilerplate-arithmetic-formatter-11#arithmetic_arranger.py

here it is, it never saves my code so you might have to copy and paste it in from the post.

Well here is one of the errors, nicely colored by the forum:

-    32         1      45      123      988
+    32         1      45      123      988    
?                                          ++++
- - 698    - 3801    + 43    +  49    +  40
+ - 698    - 3801    + 43    +  49    +  40    
?                                          ++++

There are trailing spaces at the end of your lines.
Which does make sense because .rstrip() strips spaces at the end of the string, not the end of some arbitrary linebreaks inbetween. Meaning your first_line and so on still have trailing spaces.

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