Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I am unable to find what’s wrong here. The output is showing as asked.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return print("Error: Too many problems.")
    
    top_row = ""
    bottom_row = ""
    dashes = ""
    result = ""

    for problem in problems:
        num1, operator, num2 = problem.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.')
        
        top_row += " "*(2 + max(len(num1), len(num2))-len(num1)) + num1 + " "*4
        
        bottom_row += operator + " "*(1 + max(len(num1), len(num2))-len(num2)) + num2 + " "*4
        
        dashes += '-'*(2+max(len(num1), len(num2))) + " "*4
                
        if operator == '+':
            result += " "*2 + str(int(num1) + int(num2)) + " "*4
        else:
            result += " "*2 + str(int(num1) - int(num2)) + " "*4
            
    return print(f'{top_row[:-4]}\n{bottom_row[:-4]}\n{dashes}\n{result[:-4]}')

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Have you looked at what arithmetic_arranger returns? Try wrapping the function call with the print function to see what it returns:

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

I have tried with same code but it showing “None”.

Then that’s what function returns. Per specification arithmetic_arranger should return string with arranged problems.

I got what what wrong thanks to you. But, now when I give with result it uncheck questions without result. what to do. New Code:

def arithmetic_arranger(problems):
    if len(problems) > 5:
        return "Error: Too many problems."
    
    top_row = ""
    bottom_row = ""
    dashes = ""
    result = ""

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

        if max(len(num1), len(num2)) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        
        top_row += " "*(2 + max(len(num1), len(num2))-len(num1)) + num1 + " "*4
        
        bottom_row += operator + " "*(1 + max(len(num1), len(num2))-len(num2)) + num2 + " "*4
        
        dashes += '-'*(2+max(len(num1), len(num2))) + " "*4
                
        if operator == '+':
            result += " "*2 + str(int(num1) + int(num2)) + " "*4
        else:
            result += " "*2 + str(int(num1) - int(num2)) + " "*4

    return f'{top_row[:-4]}\n{bottom_row[:-4]}\n{dashes[:-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"]))

What do you mean with this?
what’s happening now?


I’ve edited your code for readability. When you enter a code block into a forum post, 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 (').

I am trying to say that my all requirements are fulfilled except the last two, where we are require to print with the calculated answers of the given data. but if i add the program with calculated answer (result) it uncheck the other requirements where it was not needed to print.

Function needs to handle both cases, depending on the second argument passed to function.

Thanks bro or sis, I had completely forget about this parameter at later stages. I have completed the project. Thanks for all your help

1 Like