Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code generated the answer but still not pass. please help

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems)>5:
        return('Error: Too many problems.')
    
    fline =""
    sline =""
    line =""
    tline=""

    for problem in problems:
        operators = problem.split()[1]
        fnumber = problem.split()[0]
        snumber = problem.split()[2]
        maxlen = max(len(fnumber),len(snumber))
        if operators != '+' and operators !='-':
            return ("Error: Operator must be '+' or '-'.")
        if not fnumber.isdigit() or not snumber.isdigit():
            return('Error: Numbers must only contain digits.')
        if maxlen >4:
            return('Error: Numbers cannot be more than four digits.')

        if operators == '+':
            answer = int(fnumber) + int(snumber)
        if operators == '-':
            answer = int(fnumber) - int(snumber)
      
    
        fline = fline + fnumber.rjust(maxlen+2) + 4*" "
        sline = sline + operators+ " "+snumber.rjust(maxlen) + 4*" "
        line = line + '-'*(maxlen+2) + 4*" "
        
        if show_answers:
            tline = tline + str(answer).rjust(maxlen+2) + 4*" "   
    
    problems = fline + "\n" + sline + "\n" + line +"\n" + tline
        
    return problems

print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
def arithmetic_arranger(problems, show_answers=False):
    if len(problems)>5:
        return('Error: Too many problems.')
    
    fline =""
    sline =""
    line =""
    tline=""

    for problem in problems:
        operators = problem.split()[1]
        fnumber = problem.split()[0]
        snumber = problem.split()[2]
        maxlen = max(len(fnumber),len(snumber))
        if operators != '+' and operators !='-':
            return ("Error: Operator must be '+' or '-'.")
        if not fnumber.isdigit() or not snumber.isdigit():
            return('Error: Numbers must only contain digits.')
        if maxlen >4:
            return('Error: Numbers cannot be more than four digits.')

        if operators == '+':
            answer = int(fnumber) + int(snumber)
        if operators == '-':
            answer = int(fnumber) - int(snumber)
      
    
        fline = fline + fnumber.rjust(maxlen+2) + 4*" "
        sline = sline + operators+ " "+snumber.rjust(maxlen) + 4*" "
        line = line + '-'*(maxlen+2) + 4*" "
        
        if show_answers:
            tline = tline + str(answer).rjust(maxlen+2) + 4*" "   
    
    problems = fline + "\n" + sline + "\n" + line +"\n" + tline
        
    return problems

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




Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Use repr() in your function call and then compare that output with the tests:

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

You’re very close, just need to adjust it to be perfectly the same

1 Like

Thank you so much. I managed to resolve the error with the help of repr() function.

1 Like