Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

What am i missing here? I only can print one output.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return ('Error: Too many problems.')
    else:
        # Assign variable to each line
        first_line = ""
        second_line = ""
        dash_line = ""
        answer_line = ""
     # Iterate through the operation on the list:
        for i in problems:
    # Split the operation using whitespaces as separators and saving each value
            items = i.split()
            operand_1 = items[0]
            operand_2 = items[-1]
            symbol = items[1] 
     # Expected max width in each operation:
            width = max(len(operand_1), len(operand_2)) + 2
    # Digit only error:
    if not operand_1.isdigit() or not operand_2.isdigit():
        return "Error: Numbers must only contain digits."
    # Symbol specific error:
    if symbol == "+":
            answer = int(operand_1) + int(operand_2)
    elif symbol == "-":
            answer = int(operand_1) - int(operand_2)
    else:
        return "Error: Operator must be '+' or '-'."
     # Max length of digits error
    if len(operand_1) > 4 or len(operand_2) > 4:
        return "Error: Numbers cannot be more than four digits."
    # Add to each line its value in order, adjusting width to align them to the right
    first_line += str(operand_1).rjust(width)
    second_line += symbol +str(operand_2).rjust(width - 1)
    dash_line += "-" * width
    answer_line += str(answer).rjust(width)
    # In case of more than one operation, add 4 whitespaces before adding
    # the next set of numbers
    if len(problems) > 1:
        first_line += "    "
        second_line += "    "
        dash_line += "    "
        answer_line += "    "
# If second value of function set to true, it displays the answers
    if show_answers == True:
        problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line + "\n" + answer_line)
    else:
        problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line)
                
                
    return problem_arranged

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) 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

What do you mean? What’s stopping you from printing more than one?

I have coded again but q5 to 10 is still not answered why?

def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return print(‘Error: Too many problems.’)

first_line = ""
second_line = ""
dash_line = ""
answer_line = ""
 
for i in problems:
    num1, operator, num2 = i.split()
    if operator not in ('+', '-'):
        return print("Error: Operator must be '+' or '-'")
    
    if not num1.isdigit() or not num2.isdigit():
        return print('Error: Numbers must only contain digits.')

    if max(len(num1), len(num2)) > 4:
        return print('Error: Numbers cannot be more than four digits.')
    
    first_line += " "*(2 + max(len(num1), len(num2))-len(num1)) + num1 + " "*4
    
    second_line += operator + " "*(1 + max(len(num1), len(num2))-len(num2)) + num2 + " "*4
    
    dash_line += '-'*(2+max(len(num1), len(num2))) + " "*4
    if operator == '+':
        answer_line += " "*2 + str(int(num1) + int(num2)) + " " * 4
    else:
        answer_line += " "*2 + str(int(num1) - int(num2)) + " " * 4
        
return f'{first_line[:-4]}\n{second_line[:-4]}\n{dash_line[:-4]}'

print(arithmetic_arranger([“32 + 698”, “3801 - 2”, “45 + 43”, “123 + 49”]))

print(arithmetic_arranger([“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]))

you will need to write the code yourself, not copy it from other people

Copying code will result in your certification being revoked, per the Academic Honesty policy.