Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Can somebody help me find out the problem, I don’t know why the tests didn’t pass

Your code so far


Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/build-an-arithmetic-formatter-project/5e44412c903586ffb414c94c.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, 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 (').

def arithmetic_arranger(problems, show_answers=False):
    digit = '0123456789'
    operator = '+-'
    result = ''
    operator_check = []
    operand_check = []
    flag1 = 0
    flag2 = 0
    flag3 = 0

    for i in range(len(problems)):
        operator_check.append(problems[i].split()[1])
        operand_check.append(problems[i].split()[0])
        operand_check.append(problems[i].split()[-1])
    
    for i in range(len(operand_check)):
        if len(operand_check[i]) > 4:
            flag1 += 1
        for n in range(len(operand_check[i])):
            if not operand_check[i][n] in digit:
                flag2 += 1
  
    for i in operator_check:
        if not i in operator:
            flag3 += 1
    
    if len(problems) > 5:
        return 'Error: Too many problems.'
    elif flag1 > 0:
        return 'Error: Numbers cannot be more than four digits.'
    elif flag2 > 0:
        return 'Error: Numbers must only contain digits.'
    elif flag3 > 0:
        return "Error: Operator must be '+' or '-'."
    elif show_answers == True:
        my_dict = {}
        
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]

            if i < 1:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append('  ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append('  ')
                        
                else: 
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
            else:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append(6*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append(6*' ')
                else:
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            if len(my_dict[operand1]) > 1:
                result += my_dict[operand1][0] + operand1
                my_dict[operand1].pop(0)
            else:
                result += my_dict[operand1][0] + operand1

        result += '\n'
        space1 = ''

        answer = []
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if '+' in problems[i]:
                operator = '+'
                answer.append(int(operand1) + int(operand2))
            else:
                operator = '-'
                answer.append(int(operand1) - int(operand2))

            if len(operand2) >= len(operand1):
                space2 = ' '
            else:
                space2 = (abs(len(operand1) - len(operand2)) + 1)*' '

            result += space1 + operator + space2 + operand2

            if i < len(problems) - 1:
                space1 = 4*' '
        
        result += '\n'
        space_answer = []

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if len(operand1) >= len(operand2):
                dash = '-'*(len(operand1) + 2)
            else:
                dash = '-'*(len(operand2) + 2)
            space_answer.append(' '*(len(dash) - len(str(answer[i]))))
            result += dash + 4*' '

        result += '\n'

        for i in range(len(problems)):
            if i < 1:
                result += space_answer[i] + str(answer[i])
            else:
                result += 4*' ' + space_answer[i] + str(answer[i])

        return result
    else:
        my_dict = {}
        
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]

            if i < 1:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append('  ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append('  ')
                        
                else: 
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
            else:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append(6*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append(6*' ')
                else:
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            if len(my_dict[operand1]) > 1:
                result += my_dict[operand1][0] + operand1
                my_dict[operand1].pop(0)
            else:
                result += my_dict[operand1][0] + operand1

        result += '\n'
        space1 = ''

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if '+' in problems[i]:
                operator = '+'
            else:
                operator = '-'

            if len(operand2) >= len(operand1):
                space2 = ' '
            else:
                space2 = (abs(len(operand1) - len(operand2)) + 1)*' '

            result += space1 + operator + space2 + operand2

            if i < len(problems) - 1:
                space1 = 4*' '
        
        result += '\n'

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if len(operand1) >= len(operand2):
                dash = '-'*(len(operand1) + 2)
            else:
                dash = '-'*(len(operand2) + 2)
            result += dash + 4*' '
        
        return result







    








    

        

        
        
    
    
   


Tell us what’s happening:

I don’t know why my tests didn’t pass except for the error return. Can someone help me figure out?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    digit = '0123456789'
    operator = '+-'
    result = ''
    operator_check = []
    operand_check = []
    flag1 = 0
    flag2 = 0
    flag3 = 0

    for i in range(len(problems)):
        operator_check.append(problems[i].split()[1])
        operand_check.append(problems[i].split()[0])
        operand_check.append(problems[i].split()[-1])
    
    for i in range(len(operand_check)):
        if len(operand_check[i]) > 4:
            flag1 += 1
        for n in range(len(operand_check[i])):
            if not operand_check[i][n] in digit:
                flag2 += 1
  
    for i in operator_check:
        if not i in operator:
            flag3 += 1
    
    if len(problems) > 5:
        return 'Error: Too many problems.'
    elif flag1 > 0:
        return 'Error: Numbers cannot be more than four digits.'
    elif flag2 > 0:
        return 'Error: Numbers must only contain digits.'
    elif flag3 > 0:
        return "Error: Operator must be '+' or '-'."
    elif show_answers == True:
        my_dict = {}
        
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]

            if i < 1:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append('  ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append('  ')
                        
                else: 
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
            else:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append(6*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append(6*' ')
                else:
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            if len(my_dict[operand1]) > 1:
                result += my_dict[operand1][0] + operand1
                my_dict[operand1].pop(0)
            else:
                result += my_dict[operand1][0] + operand1

        result += '\n'
        space1 = ''

        answer = []
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if '+' in problems[i]:
                operator = '+'
                answer.append(int(operand1) + int(operand2))
            else:
                operator = '-'
                answer.append(int(operand1) - int(operand2))

            if len(operand2) >= len(operand1):
                space2 = ' '
            else:
                space2 = (abs(len(operand1) - len(operand2)) + 1)*' '

            result += space1 + operator + space2 + operand2

            if i < len(problems) - 1:
                space1 = 4*' '
        
        result += '\n'
        space_answer = []

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if len(operand1) >= len(operand2):
                dash = '-'*(len(operand1) + 2)
            else:
                dash = '-'*(len(operand2) + 2)
            space_answer.append(' '*(len(dash) - len(str(answer[i]))))
            result += dash + 4*' '

        result += '\n'

        for i in range(len(problems)):
            if i < 1:
                result += space_answer[i] + str(answer[i])
            else:
                result += 4*' ' + space_answer[i] + str(answer[i])

        return result
    else:
        my_dict = {}
        
        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]

            if i < 1:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append('  ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append('  ')
                        
                else: 
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 2)*' ')
            else:
                if len(operand1) >= len(operand2):
                    if operand1 in my_dict:
                        my_dict[operand1].append(6*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append(6*' ')
                else:
                    if operand1 in my_dict:
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')
                    else:
                        my_dict[operand1] = []
                        my_dict[operand1].append((abs(len(operand1) - len(operand2)) + 6)*' ')

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            if len(my_dict[operand1]) > 1:
                result += my_dict[operand1][0] + operand1
                my_dict[operand1].pop(0)
            else:
                result += my_dict[operand1][0] + operand1

        result += '\n'
        space1 = ''

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if '+' in problems[i]:
                operator = '+'
            else:
                operator = '-'

            if len(operand2) >= len(operand1):
                space2 = ' '
            else:
                space2 = (abs(len(operand1) - len(operand2)) + 1)*' '

            result += space1 + operator + space2 + operand2

            if i < len(problems) - 1:
                space1 = 4*' '
        
        result += '\n'

        for i in range(len(problems)):
            operand1 = problems[i].split()[0]
            operand2 = problems[i].split()[-1]
            if len(operand1) >= len(operand2):
                dash = '-'*(len(operand1) + 2)
            else:
                dash = '-'*(len(operand2) + 2)
            result += dash + 4*' '
        
        return result

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/build-an-arithmetic-formatter-project/5e44412c903586ffb414c94c.md at main · freeCodeCamp/freeCodeCamp · GitHub

you can check your output vs the expected one using this: (repr puts the string all on line for easier comparison)

print("actual  ", repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
print("expected", repr("  3801      123\n-    2    +  49\n------    -----"))

currently it prints this:

actual   '  3801      123\n-    2    +  49\n------    -----    '
expected '  3801      123\n-    2    +  49\n------    -----'

do you see the differences between the two strings?

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.