Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I cannot figure out why when I run the code it only shows one formatted problem and no answer (Even when show_answers is True)

Your code so far

def arithmetic_arranger(problems, show_answers=True):
    line1 = ''
    line2 = ''
    line_equal = ''
    line_result = ''
    if len(problems) > 5:
        return(f'Error: Too many problems.')
    for problem in problems:
        parts = num1 , operator , num2 = problem.split()
        
    if operator not in ['+','-']:
        return(f'Error: Operator must be "+" or "-".')
    if len(num1) > 4 or len(num2) > 4:
        return(f'Error: Numbers cannot be more than four digits.')
    if not num1.isdigit() or not num2.isdigit():
        return(f'Error: Numbers must only contain digits.')
    if show_answers:
        if operator == '+':
            result = str(int(num1) + int(num2))
        elif operator == '-':
            result = str(int(num1) - int(num2))
    max_len = max(len(num1), len(num2)) + 2
    line1 += num1.rjust(max_len) + '  '
    line2 += operator + '  ' + num2.rjust(max_len - 2)
    line_equal += '_' * max_len + '  '
    if show_answers:
        line_result += result.rjust(max_len) + '  '
    formatted_problems = str(line1 + '\n' + line2 + '\n' + line_equal)
    return formatted_problems


print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Add some print statements to troubleshoot your code.

Check the line where the result is added to the final output

for problem in problems:
        parts = num1 , operator , num2 = problem.split()

Print your parts variable here after the loop to make sure it has worked as expected

1 Like

Thank you! I see now that it only gives one of the problems now, I’ll work to fix it!

1 Like

Solved part of the problem

def arithmetic_arranger(problems, show_answers=True):
    line1 = ''
    line2 = ''
    line_equal = ''
    line_result = ''
    if len(problems) > 5:
        return(f'Error: Too many problems.')
    for problem in problems:
        parts = problem.split()
        num1 = parts[0]
        operator =parts[1]
        num2 = parts[2]

When I run just that part of the program with print(num1) , I see all the first numbers of each equation under one another.

But when I try the same exact command at the end of my function, the only number it gives me is 123 (The last equation’s first number)

I tried changing/deleting other parts of the code but every time I exit the for-loop and do print(num1) , I only get 123 instead of all the first numbers.

Why does it do that and how do i fix it?

num1 is a variable just storing one number and getting overwritten everytime it’s assigned. Try using something that holds multiple indexed values (as you’ve done with parts) and append new values.

1 Like