Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I have been stuck in the Tests. I don’t know what is my mistake! when I print it in console everything is ok however I would like some feedback to fix it.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    dash = ''
    row1=''
    row2=''
    problems_copy = problems[:]
    results = ''
    string=''
    space = ' '*4
    for problem in problems_copy:
        num1, operation, num2 = problem.split(' ')
        if len(num1) > len(num2):
            longer_value = len(num1)
        else:
            longer_value = len(num2)
        try:
            if len(problems) > 5:
                    return 'Error: Too many problems.'
            elif len(num1) > 4 or len(num2) > 4:
                    return 'Error: Numbers cannot be more than four digits.'
            elif not num1.isdigit() or not num2.isdigit():
                    return 'Error: Numbers must only contain digits.'
            
            elif operation == '+':
                max_char = longer_value + 2
                if show_answers:
                    results += ' '*(max_char-longer_value)+str(int(num1) + int(num2)) + space
                row1 += " "*(max_char-len(num1))+str(num1) + space 
                row2 += operation+ ' '*(max_char-len(num2)-1) +str(num2) + space
                dash += '-'*(max_char) + space
                
            
            elif operation == '-':
                max_char = longer_value + 2
                if show_answers:
                    results += ' '*(max_char-longer_value)+str(int(num1) - int(num2)) + space
                row1 += ' '*(max_char-len(num1))+str(num1) + space
                row2 += operation + ' '*(max_char-len(num2)-1) +str(num2) + space
                dash += '-'*(max_char) + space
                
            elif operation == '/' or operation == '*':
                    return "Error: Operator must be '+' or '-'."
            
            
            problems.pop(0)
        
        except Exception as e:
             print(e)
    #string += row1 + row2 + dash
    if show_answers:
        string += row1 + row2 + dash + results
    else:
        string += row1 + row2 + dash
    problems.append(string)
    print(row1 + "\n" + row2 + "\n" + dash + '\n' + results)
    return problems

print(f'\n{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Add repr() to your print, it will help compare your output to the tests.

`print(repr(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])))`

Also check the devtools console (F12) for more verbose test output

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