Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I don’t think my problems are splitting correctly, but I’m not sure how to tell for sure if that’s the problem, or how to fix it. Some of my problems I think are showing up with multiple operators, and they also aren’t spacing properly. I can’t figure out how to add the spacing so its even between each problem and the numbers line up.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    #variables
    first_number = ''
    operator = ''
    second_number = ''
    lines = ''
   
    if len(problems) > 5:
        return 'Error: Too many problems.'
   
    for problem in problems:
        problem_array = problem.split()
        first_number = problem_array[0]
        operator = problem_array[1]
        second_number = problem_array[2]
        
        #error message 2 and 4 and operator solutions
        if operator == '+':
            if show_answers == True:
                solution = int(first_number) + int(second_number)
        if operator == '-':
            if show_answers == True:
                solution = int(first_number) - int(second_number)
        else:
            return "Error: Operator must be '+' or '-'."
        
        if show_answers == False:
            solution = ''
        
        if first_number.isdigit() and second_number.isdigit():
            if len(first_number) > 4:
                return 'Error: Numbers cannot be more than four digits.'
            if len(second_number) > 4:
                return 'Error: Numbers cannot be more than four digits'
        else:
            return 'Error: Numbers must only contain digits.'

        #formatting problem parts
        top_line = 'f{first_number.rjust()}'
        bottom_line = 'f{operator.ljust()} {second_number.rjust()}'
        if len(first_number) > len(second_number):
            if len(first_number) == 2:
                lines = '----'
            elif len(first_number) == 3:
                lines = '-----'
            elif len(first_number) == 4:
                lines = '------'
        if len(second_number) > len(first_number):
            if len(second_number) == 2:
                lines = '----'
            elif len(second_number) == 3:
                lines = '-----'
            elif len(second_number) == 4:
                lines = '------'

        #formatting whole problem
        whole_problem = 'f{top_line}\n{bottom_line}\n{lines}\n{solution}'
    return '    '.join(whole_problem)
    
arithmetic_arranger(["3801 - 2", "123 + 49"])

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Looking at the extended test output in the console (F12), there seems to be the issue that Error: Operator must be '+' or '-'. is returned in many situations where it should not be.

When you fix that, you will want to remeber where the f goes when you write an f-string

Tell us what’s happening:

my problems are all appearing vertically stacked instead of on the same line. What is making them do that? Also, my more than four digits error isn’t working, but I can’t figure out why that is.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    #variables
    first_number = ''
    operator = ''
    second_number = ''
    lines = ''
   
    if len(problems) > 5:
        return 'Error: Too many problems.'
   
    for problem in problems:
        problem_array = problem.split()
        first_number = problem_array[0]
        operator = problem_array[1]
        second_number = problem_array[2]
        
        #error message 2 and 4 and operator solutions
        if operator == '+':
            if show_answers == True:
                solution = str(int(first_number) + int(second_number))
        elif operator == '-':
            if show_answers == True:
                solution = str(int(first_number) - int(second_number))
        else:
            return "Error: Operator must be '+' or '-'."
        
        if show_answers == False:
            solution = ''
        
        if first_number.isdigit() and second_number.isdigit():
            if len(first_number) > 4:
                return 'Error: Numbers cannot be more than four digits.'
            if len(second_number) > 4:
                return 'Error: Numbers cannot be more than four digits'
        else:
            return 'Error: Numbers must only contain digits.'

        #formatting problem parts
        shift = max(len(first_number), len(second_number)) + 2
        top_line = first_number.rjust(shift)
        bottom_line = operator + second_number.rjust(shift-1
        )
        num_dashes = int(shift)
        dashes = '-'
        lines = dashes.rjust(num_dashes, '-')
        answer = solution.rjust(shift)
        #spacing
        final_top = top_line + '    '
        final_bottom = bottom_line + '    '
        final_lines = lines + '    '
        final_answer = answer + '    '

        #formatting whole problem
        if show_answers == False:
            whole_problem = final_top + '\n' + final_bottom + '\n' + final_lines
        else:
            whole_problem = final_top + '\n' + final_bottom + '\n' + final_lines + '\n' + final_answer
        final_problems = print(f'{whole_problem}')
    return final_problems


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Actually, I figured out there was just a typo with the error message, so I just need help with the vertical stacking/spacing.

Your function is printing, not returning.

Comment out this line:

final_problems = print(f'{whole_problem}')

Add this line at the bottom to test/troubleshoot:

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

Then you will see what your code is really returning. Figuring out the printing is really the main task of the project, it’s no something we can help much with.

You need to print it out like a dot matrix printer. The problem is in your loop where you build your final string, you aren’t appending. You are replacing the whole_problem instead of adding onto it.

Another tip is to use repr() to see your output in the same format the tests show you.

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
>>> Error: Operator must be '+' or '-'.

This doesn’t seem right.

You need to print your variables at various stages in your function to troubleshoot, otherwise you are just guessing at what’s happening.

I don’t think my problems are splitting correctly, but I’m not sure how to tell for sure if that’s the problem, or how to fix it.

Print the problems when you split it to see if it’s splitting correctly. Every time you do something print the variable, your code should be full of print() statements to troubleshoot.

If you are passing [“3801 - 2”, “123 + 49”] as function arguments, just before the last return statement, add:

print(r"  3801      123\n-    2    +  49\n------    -----")
print(final_problems.replace("\n","\\n"))

Make sure both printings are the same. From there, you can figure out what went wrong.