Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I feel like my code is returning what is asked but failing the tests for some reason. I know my code is long, cumbersome and probably repetitive but it does work.

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    splitList = []
    numList = []
    symList = []
    solList = []
    testNumList = []

    # Split up lists to be more manageable

   

    for string in problems:
        splitList.extend(string.split())

    for string in splitList:
        if string.isdigit():
            numList.append(string)

    for item in splitList:
        if item.isdigit() == False and item != "-" and item != "+":
            print('Error: Numbers must only contain digits.')

    symList = splitList[1::3]

         
    # Develop error statements

    if len(splitList) > 15:
        print('Error: Too many problems')    

    for char in symList:
        if char != '+' and char != '-':
            print("Error: Operator must be '+' or '-'.")
            break

    for num in numList:
        if len(num) > 4:
            print('Error: Numbers cannot be more than four digits.')

    # Divide up the equations

    equOne = numList[0:2]
    equTwo = numList[2:4]
    equThree = numList[4:6]
    equFour = numList[6:8]
    equFive = numList[8:10]

    # Equation calculations
    
    solOne = '      '
    solTwo = '      '
    solThree = '      '
    solFour = '      '
    solFive = '      '
    
    for i in range(len(symList)):
        if i == 0:
            if symList[0] == "+":
                solOne = int(equOne[0]) + int(equOne[1])
            else:
                solOne = int(equOne[0]) - int(equOne[1])
            
        elif i == 1:
            if symList[1] == "+":
                solTwo = int(equTwo[0]) + int(equTwo[1])
            else:
                solTwo = int(equTwo[0]) - int(equTwo[1])
        
        elif i == 2:
            if symList[2] == "+":
                solThree = int(equThree[0]) + int(equThree[1])
            else:
                solThree = int(equThree[0]) - int(equThree[1])
        
        elif i == 3:
            if symList[3] == "+":
                solFour = int(equFour[0]) + int(equFour[1])
            else:
                solFour = int(equFour[0]) - int(equFour[1])
                
        elif i == 4:       
            if symList[4] == "+":
                solFive = int(equFive[0]) + int(equFive[1])
            else:
                solFive = int(equFive[0]) - int(equFive[1])
                
    # Calculate the spacing between symbols and denomenators.

    # "n" is the space between the symbol and the denomenator.
    # "ns" is the right justification spacing for the numerator.
    # "ds" is the right justification spacing for the denomenator.
    
    for i in range(len(symList)):
        if i == 0:
            if len(equOne[0]) > len(equOne[1]) and len(equOne[0]) - len(equOne[1]) == 3:
                n0 = '    '
                s0 = 1 + len(n0) + len(equOne[1])
            elif len(equOne[0]) > len(equOne[1]) and len(equOne[0]) - len(equOne[1]) == 2:
                n0 = '   '
                s0 = 1 + len(n0) + len(equOne[1])
            elif len(equOne[0]) > len(equOne[1]) and len(equOne[0]) - len(equOne[1]) == 1:
                n0 = '  '
                s0 = 1 + len(n0) + len(equOne[1])
            else:
                n0 = ' '
                s0 = 1 + len(n0) + len(equOne[1])
        
        elif i == 1:    
            if len(equTwo[0]) <= len(equTwo[1]):
                n1 = ' '
                ds1 = len(equTwo[1])
                ns1 = len(equTwo[1]) + 6
            else:
                n1 = ' '
                ds1 = len(equTwo[0])
                ns1 = len(equTwo[0]) + 6
            
        elif i == 2:
            if len(equThree[0]) <= len(equThree[1]):
                n2 = ' '
                ds2 = len(equThree[1])
                ns2 = len(equThree[1]) + 6
            else:
                n2 = ' '
                ds2 = len(equThree[0])
                ns2 = len(equThree[0]) + 6
        
        elif i == 3:
            if len(equFour[0]) <= len(equFour[1]):
                n3 = ' '
                ds3 = len(equFour[1])
                ns3 = len(equFour[1]) + 6
            else:
                n3 = ' '
                ds3 = len(equFour[0])
                ns3 = len(equFour[0]) + 6       

        elif i == 4:
            if len(equFive[0]) <= len(equFive[1]):
                n4 = ' '
                ds4 = len(equFive[1])
                ns4 = len(equFive[1]) + 6
            else:
                n4 = ' '
                ds4 = len(equFive[0])
                ns4 = len(equFive[0]) + 6

        


    #Calculate the bottom line (literally)

    for i in range(len(symList)):
        if i == 0:
            if len(equOne[0]) == 4 or len(equOne[1]) == 4:
                bl1 = '------'
            elif len(equOne[0]) == 3 or len(equOne[1]) == 3:
                bl1 = '-----'
            elif len(equOne[0]) == 2or len(equOne[1]) == 2:
                bl1 = '----'
            else:
                bl1 = '---'
    
        elif i == 1:    
            if len(equTwo[0]) == 4 or len(equTwo[1]) == 4:
                bl2 = '------'
            elif len(equTwo[0]) == 3 or len(equTwo[1]) == 3:
                bl2 = '-----'
            elif len(equTwo[0]) == 2 or len(equTwo[1]) == 2:
                bl2 = '----'
            else:
                bl2 = '---'

        elif i == 2:    
            if len(equThree[0]) == 4 or len(equThree[1]) == 4:
                bl3 = '------'
            elif len(equThree[0]) == 3 or len(equThree[1]) == 3:
                bl3 = '-----'
            elif len(equThree[0]) == 2 or len(equThree[1]) == 2:
                bl3 = '----'
            else:
                bl3 = '---'

        elif i == 3:
            if len(equFour[0]) == 4 or len(equFour[1]) == 4:
                bl4 = '------'
            elif len(equFour[0]) == 3 or len(equFour[1]) == 3:
                bl4 = '-----'
            elif len(equFour[0]) == 2 or len(equFour[1]) == 2:
                bl4 = '----'
            else:
                bl4 = '---'

        elif i == 4:
            if len(equFive[0]) == 4 or len(equFive[1]) == 4:
                bl5 = '------'
            elif len(equFive[0]) == 3 or len(equFive[1]) == 3:
                bl5 = '-----'
            elif len(equFive[0]) == 2 or len(equFive[1]) == 2:
                bl5 = '----'
            else:
                bl5 = '---'

    if show_answers == False:
        solOne = '      '
        solTwo = '      '
        solThree = '      '
        solFour = '      '
        solFive = '      '

# Print solutions

    if (len(symList)) == 1:
        print(f'{numList[0]:>{s0}}\n{symList[0]}{n0}{numList[1]}\n{bl1}\n{solOne:>{s0}}')
    elif (len(symList)) == 2:
        print(f'{numList[0]:>{s0}}{numList[2]:>{ns1}}\n{symList[0]}{n0}{numList[1]}    {symList[1]}{n1}{numList[3]:>{ds1}}\n{bl1}    {bl2}\n{solOne:>{s0}}{solTwo:>{ns1}}')
    elif (len(symList)) == 3:
        print(f'{numList[0]:>{s0}}{numList[2]:>{ns1}}{numList[4]:>{ns2}}\n{symList[0]}{n0}{numList[1]}    {symList[1]}{n1}{numList[3]:>{ds1}}    {symList[2]}{n2}{numList[5]:>{ds2}}\n{bl1}    {bl2}    {bl3}\n{solOne:>{s0}}{solTwo:>{ns1}}{solThree:>{ns2}}')
    elif (len(symList)) == 4:
        print(f'{numList[0]:>{s0}}{numList[2]:>{ns1}}{numList[4]:>{ns2}}{numList[6]:>{ns3}}\n{symList[0]}{n0}{numList[1]}    {symList[1]}{n1}{numList[3]:>{ds1}}    {symList[2]}{n2}{numList[5]}    {symList[3]}{n3}{numList[7]}\n{bl1}    {bl2}    {bl3}    {bl4}\n{solOne:>{s0}}{solTwo:>{ns1}}{solThree:>{ns2}}{solFour:>{ns3}}')
    elif (len(symList))  == 5:
        print(f'{numList[0]:>{s0}}{numList[2]:>{ns1}}{numList[4]:>{ns2}}{numList[6]:>{ns3}}{numList[8]:>{ns4}}\n{symList[0]}{n0}{numList[1]}    {symList[1]}{n1}{numList[3]:>{ds1}}    {symList[2]}{n2}{numList[5]}    {symList[3]}{n3}{numList[7]}    {symList[4]}{n4}{numList[9]}\n{bl1}    {bl2}    {bl3}    {bl4}    {bl5}\n{solOne:>{s0}}{solTwo:>{ns1}}{solThree:>{ns2}}{solFour:>{ns3}}{solFive:>{ns4}}')

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/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum @savedbygrace_88

I looked in the console and saw a syntax error.

Happy coding

I’ve edited your code for readability. 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 (').

AssertionError: None 
             != '   32         1      45      123      98[126 chars]1028'

why is the output from your function None? it needs to return a string

Thank you for your help! I have passed!