Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I know my problem is that I am printing the final answer and not returning it. I just cannot seem to figure out the difference between print and return. Please help!

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    line_1 = ''
    line_2 = ''
    line_3 = ''
    line_4 = ''

#Error Codes    
    if len(problems)>5: 
        return 'Error: Too many problems.'

    for problem in problems:
        num1, operator, num2 = problem.split()
                
        if (operator == '*') or (operator == '/') :
            return "Error: Operator must be '+' or '-'."

        if (len(num1)>4) or (len(num2)>4):
            return 'Error: Numbers cannot be more than four digits.'
        
        for char in problem: 
            if char.isalpha():
                return 'Error: Numbers must only contain digits.'

        num1_len = len(num1)
        num2_len = len(num2)
        long_num = max(num1_len, num2_len)
        num1_space = (long_num + 2) - num1_len
        num2_space = (long_num +1) - num2_len
        problem_spacing = ' '*4
        
        if operator == '+':
            answer = int(num1) + int(num2)
        elif operator == '-':
            answer = int(num1) - int(num2)
        
        answer_len = len(str(answer))
        answer_space = (long_num + 2) - answer_len
        
        line_1 += ' '*num1_space + num1 + ' '*4
        line1 = line_1
        line_2 += operator + ' '*num2_space + num2 + ' '*4
        line2 = line_2
        line_3 += '-'*(long_num + 2) + ' '*4 
        line3 = line_3
        line_4 += ' '*answer_space + str(answer) + ' '*4
        line4 = line_4

        if show_answers == True: 
            final_answer = f'{line_1}\n{line_2}\n{line_3}'
        else: 
            final_answer = f'{line_1}\n{line_2}\n{line_3}\n{line_4}'
    print(final_answer)

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/133.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Hi and welcome to the forum :wave:

This is a return:

This is a print:

Your function should never have a print statement, only returns

To test your function you can call it within a print like this

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

def arithmetic_arranger(problems, show_answers=False):

    line_1 = ''
    line_2 = ''
    line_3 = ''
    line_4 = ''

    if len(problems)>5: 
        return 'Error: Too many problems.'

    for problem in problems:
        num1, operator, num2 = problem.split()
                
        if (operator == '*') or (operator == '/') :
            return "Error: Operator must be '+' or '-'."

        if (len(num1)>4) or (len(num2)>4):
            return 'Error: Numbers cannot be more than four digits.'
        
        for char in problem: 
            if char.isalpha():
                return 'Error: Numbers must only contain digits.'

        num1_len = len(num1)
        num2_len = len(num2)
        long_num = max(num1_len, num2_len)
        num1_space = (long_num + 2) - num1_len
        num2_space = (long_num +1) - num2_len
        problem_spacing = ' '*4
        
        if operator == '+':
            answer = int(num1) + int(num2)
        elif operator == '-':
            answer = int(num1) - int(num2)
        
        answer_len = len(str(answer))
        answer_space = (long_num + 2) - answer_len
        
        line_1 += ' '*num1_space + num1 + ' '*4
        line1 = line_1
        line_2 += operator + ' '*num2_space + num2 + ' '*4
        line2 = line_2
        line_3 += '-'*(long_num + 2) + ' '*4 
        line3 = line_3
        line_4 += ' '*answer_space + str(answer) + ' '*4
        line4 = line_4

        if show_answers == True: 
            final_answer = f'{line_1}\n{line_2}\n{line_3}\n{line_4}'
        elif show_answers == False: 
            final_answer = f'{line_1}\n{line_2}\n{line_3}'
    return final_answer

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

Alright this is what I have now, but it still isn’t passing any tests other than the error codes.

Great, you are almost there.

Use repr() to compare the raw string output of your function to the expected output:

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

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

arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ -----

Compare them like this:

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

You should have a newline immediately after 123 but you have some extra spaces at the end of each line.

Thank you so much! Those extra spaces almost made me go crazy…

1 Like

Also check this note:

Note: open the browser console with F12 to see a more verbose output of the tests.