Can't pass tests - Arithmetic Formatter

When I use my code in an IDE, I “pass” the tests but on FCC I do not. I definitely think it’s formatted right but obviously there is something wrong.

please just some hints would be nice I don’t get it at all???

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    top_rows = []
    bottom_rows = []
    symbol_rows = []
    answers = []
        
    if len(problems) > 5:
        print("Error: Too many problems.")  
        
    for problem in problems:                            
        operands = problem.split()                        
        first_operands, operators, second_operands = operands
       
        if operators not in ["+", "-"]:                  
            print("Operator must be '+' or '-'.")
            continue
         
        if not (first_operands.strip().isdigit() and second_operands.strip().isdigit()):
            print("Numbers must only contain digits.")
       
        if len(first_operands) > 4 or len(second_operands) > 4 :
            print("Numbers cannot be more than four digits.")
            
        width = max(len(first_operands), len(second_operands)) + 2

        top_rows.append(first_operands.rjust(width))
        bottom_rows.append(f"{operators} {second_operands.rjust(width - 2)}")
        

        symbol_rows.append("-" * width)
        
        aligned_top =  "    ".join(top_rows)
        aligned_bottom =  "    ".join(bottom_rows)
        aligned_symbols =  "    ".join(symbol_rows)
        
        if show_answers:
            
            if operators == "+":
                answer = int(first_operands) + int(second_operands)
                    
            elif operators == "-":
                answer = int(first_operands) - int(second_operands)
            answers.append(str(answer).rjust(width))
          
        #largest_operand = max(len(str(first_operands)), len(str(second_operands)))
    aligned_top =     "    ".join(top_rows)
    aligned_bottom =  "    ".join(bottom_rows)
    aligned_symbols = "    ".join(symbol_rows)
    aligned_answers = "    ".join(answers)
    
    
    
    if show_answers:
        print(f"{aligned_top}\n{aligned_bottom}\n{aligned_symbols}\n{aligned_answers}")
        
    else:
        print(f"{aligned_top}\n{aligned_bottom}\n{aligned_symbols}")



    return problems




arithmetic_arranger(["3801 - 2", "123 + 49"])

Challenge: Step 40

Link to the challenge:

What your function returns?

arithmetic_arranger([“3801 - 2”, “123 + 49”,“3801 - 2”, “123 + 49”,“3801 - 2”, “123 + 49”], show_answers=True)
this call returns:

Error: Too many problems.
  3801      123      3801      123      3801      123
-    2    +  49    -    2    +  49    -    2    +  49
------    -----    ------    -----    ------    -----
  3799      172      3799      172      3799      172

I’ve now fixed the error handling tests by changing my print statements to return and then and a print statement onto my call function but the formatting must be wrong somewhere.

I have now completed it. I forgot to also change the print statements for my “aligned_top/bottom/symbols” to return. sorry for wasting your time but I just need to take a break and come back to it.

2 Likes