Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I’ve tried everything and the output is the same as what the tests are asking for but they do not work except for the tests on errors.

Your code so far

def format (lines1, lines2, lines3, totals = ""):
    lines = ""
    line1 = ""
    line2 = ""
    line3 = "" 
    line4 = ""
    for line in lines1: 
        line1 += (line) 
    for line in lines2:
        line2 += (line)
    for line in lines3:
        line3 += (line)
    for total in totals:
        line4 += total
    lines += line1 + "\n"
    lines+= line2 + "\n"
    lines+= line3 + "\n"
    lines+= line4 + "\n"
    
    return lines
    
def sort_equations(problems, show_answers = False):
    error = ""
    lines1 = []
    lines2 = []
    lines3 = []
    lines4 = []
    #seperate and sort needed values
    digit0 =[]
    digit1 = [] 
    for problem in problems:
        error = ""
        digit = []
        prob = problem.split()
        op = ""
        for p in prob:
            if p.isnumeric():
                digit.append(p)
            elif p == "+":
                op = "+"
            elif p == "-":
                op = "-"
        if op == "":
            error = "Error: Operator must be '+' or '-'."
        if len(digit) != 2:
            error = "Error: Numbers must only contain digits."
        for d in digit:
            if len(d) > 4:
                error = "Error: Numbers cannot be more than four digits."
        if error != "":
            return error
        if op == "+":
            total = int(digit[0]) + int(digit[1])
        elif op == "-":
            total = int(digit[0]) - int(digit[1])
        total = str(total)
        digit0.append(digit[0]) 
        digit1.append(digit[1])
        #find longest length for num of spaces and dashes needed
       
        if len(digit[0]) > len(digit[1]):
            longest = len(digit[0])
            diff = len(digit[0]) - len(digit[1])
        elif len(digit[0]) < len(digit[1]):
            longest = len(digit[1])
            diff = len(digit[1]) - len(digit[0])
        else:
            longest = len(digit[0])
            diff = 0
        #find spaces needed
        spaces = " "
        dashes = ""
        for i in range(longest + 2):
            dashes += "-"
        for i in range(diff):
            spaces += " "
        sp = len(dashes) - len(total)
        ans_sp = ""
        for i in range(sp):
            ans_sp += " "
        #format everything together
        if longest == len(digit[0]):
            line1 = "  " + digit[0] + "    "
            line2 = op+ spaces + digit[1] + "    "
            line3 = dashes + "    "
            line4 = ans_sp + total + "    "
        elif longest == len(digit[1]):
            line1 = " " +spaces + digit[0] + "    "
            line2 = op + " " + digit[1]+"    "
            line3 = dashes + "    "
            line4 = ans_sp + total+ "    "
        lines1.append(line1)
        lines2.append(line2)
        lines3.append(line3)
        lines4.append(line4)
    if show_answers == True:
        lines = format(lines1, lines2, lines3,lines4)
    else:
        lines = format(lines1, lines2, lines3)
    return lines

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        if show_answers == True:
            lines = sort_equations(problems, True)
        else:
            lines = sort_equations(problems)
        return lines

def main(): 
    print("------Arithmetic Formatter------")
    print("\n")
    
    #Test1
    print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
    
    #Test2
    print(f'\n{arithmetic_arranger(["1 + 2", "1 - 9380"])}')
    
    #Test3
    print(f'\n{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
    
    #Test4
    print(f'\n{arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])}')
    
    #Test5
    print(f'\n{arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])}')
    
    #Test6
    print(f'\n{arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])}')
    
    #Test7
    print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')
    
    #Test8
    print(f'\n{arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])}')
    
    #Test9
    print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
    
    #Test10
    print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')


main() 


Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I’ve edited your post to improve the readability of the code. 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 (').

try this:

def main(): 
    print("------Arithmetic Formatter------")
    print("\n")
    
    #Test1
    print('Test 1')
    print('arithmetic_arranger(["3801 - 2", "123 + 49"])')
    print('Actual  :', repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
    print('Expected:', repr('  3801      123\n-    2    +  49\n------    -----'))
    print()
    
    #Test2
    print('Test 2')
    print('arithmetic_arranger(["1 + 2", "1 - 9380"])')
    print('Actual  :', repr(arithmetic_arranger(["1 + 2", "1 - 9380"])))
    print('Expected:', repr('  1         1\n+ 2    - 9380\n---    ------'))
    print()
    
    #Test3
    print('Test 3')
    print('arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])')
    print('Actual  :', repr(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])))
    print('Expected:', repr('    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'))
    print()
    
    #Test4
    print('Test 4')
    print('arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])')
    print('Actual  :', repr(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])))
    print('Expected:', repr('  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'))
    print()
    
    #Test5
    print('Test 5')
    print('arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])')
    print('Actual  :', repr(arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])))
    print('Expected:', repr('Error: Too many problems.'))
    print()
    
    #Test6
    print('Test 6')
    print('arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])')
    print('Actual  :', repr(arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])))
    print('Expected:', repr("Error: Operator must be '+' or '-'."))
    print()
    
    #Test7
    print('Test 7')
    print('arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])')
    print('Actual  :', repr(arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])))
    print('Expected:', repr('Error: Numbers cannot be more than four digits.'))
    print()
    
    #Test8
    print('Test 8')
    print('arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])')
    print('Actual  :', repr(arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])))
    print('Expected:', repr('Error: Numbers must only contain digits.'))
    print()
    
    #Test9
    print('Test 9')
    print('arithmetic_arranger(["3 + 855", "988 + 40"], True)')
    print('Actual  :', repr(arithmetic_arranger(["3 + 855", "988 + 40"], True)))
    print('Expected:', repr('    3      988\n+ 855    +  40\n-----    -----\n  858     1028'))
    print()
    
    #Test10
    print('Test 10')
    print('arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)')
    print('Actual  :', repr(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)))
    print('Expected:', repr('   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'))
    print()


main()

so you will see better what is coming from each function call and compare it more easily

1 Like

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