Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

my code cant seem to pass on tests example, but it passes on the preview menu,
Where did i get wrong or which line is not right?

please help me.

Your code so far

def arithmetic_arranger(problems, show_answers = True):
    if len(problems) > 5 :
        return "Error: Too many problems."
    # Initialize variables
    first_line = ""
    second_line = ""
    dashes = ""
    answers = ""
    def operator_func( num1 , operator,num2):
        if operator not in ['-','+']:
            return 'Error: Operator must only contain digits.'
        if operator == '+':
            return int(num1) + int(num2)
        elif operator == '-':
            return int(num1) - int(num2)
    # Loop through problems
    for problem in problems:
        num1, operator, num2 = problem.split()
        
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits"
        if len(num1) >4 or len(num2)>4:
            return'Error: Number cannot be more than four digits'
   

        # Format problem strings
    max_len = max(len(num1), len(num2)) + 2
    first_line += num1.rjust(max_len)
    second_line += operator + " " + num2.rjust(max_len - 2)
    dashes += "-" * max_len
        

    # Return arranged problems
    if show_answers:
        return first_line + "\n" + second_line + "\n" + dashes + "\n" + answers
    else:
        return first_line + "\n" + second_line + "\n" + dashes

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



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

it doesn’t pass on the previous, you have two function calls

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

and the preview shows only one problem for each

  123
+  49
-----

     1
- 9380
------

you are missing the first problem for both calls

how do i call the first problem, anothewr thinf thers no answer showing

i have added the zswer line : answers += str(problem).rjust(max_len)
but it show 988 +40

I don’t understand your question

im getting
988

  • 40

988 + 40
instead of 1028

which part of your code is dedicated to calculating the sum?

f show_answers:
answer = operator_func(num1, operator, num2)
answers += str(answers).rjust(max_len) + " "

can you post your full code again? I don’t see a call to operator_func in the code you posted earlier

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 (').

def arithmetic_arranger(problems, show_answers = True):
    if len(problems) > 5 :
        return "Error: Too many problems."
    # Initialize variables
    first_line = ""
    second_line = ""
    dashes = ""
    answers = ""
    def operator_func( num1 , operator,num2):
        if operator not in ['-','+']:
            return 'Error: Operator must only contain digits.'
        if operator == '+':
            return int(num1) + int(num2)
        elif operator == '-':
            return int(num1) - int(num2)
    # Loop through problems
    for problem in problems:
        num1, operator, num2 = problem.split()
        
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits"
        if len(num1) >4 or len(num2)>4:
            return'Error: Number cannot be more than four digits'
   

    # Format problem strings
    max_len = max(len(num1), len(num2)) + 2
    first_line += num1.rjust(max_len) + " "
    second_line += operator + " " + num2.rjust(max_len - 2) + "  "
    dashes += "-" * max_len + "  " 
    answers += str(problem).rjust(max_len)
        

    # Return arranged problems
    if show_answers:
        answer = operator_func(num1, operator, num2)
        answers += str(answers).rjust(max_len) + "  "



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

Indentation is really important in python, please use the backticks to format your code so that it is readable.

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 (').

you do not have a return anymore, it’s quite difficult to review incomplete code

def arithmetic_arranger(problems, show_answers = True):
    if len(problems) > 5 :
        return "Error: Too many problems."
    # Initialize variables
    first_line = ""
    second_line = ""
    dashes = ""
    answers = ""
    def operator_func( num1 , operator,num2):
        if operator not in ['-','+']:
            return 'Error: Operator must only contain digits.'
        if operator == '+':
            return int(num1) + int(num2)
        elif operator == '-':
            return int(num1) - int(num2)
    # Loop through problems
    for problem in problems:
        num1, operator, num2 = problem.split()
        
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits"
        if len(num1) >4 or len(num2)>4:
            return'Error: Number cannot be more than four digits'
   

    # Format problem strings
    max_len = max(len(num1), len(num2)) + 2
    first_line += num1.rjust(max_len) + " "
    second_line += operator + " " + num2.rjust(max_len - 2) + "  "
    dashes += "-" * max_len + "  " 
    answers += str(problem).rjust(max_len)
        

    # Return arranged problems
    if show_answers:
        answer = operator_func(num1, operator, num2)
        answers += str(answers).rjust(max_len) + "  "
    return answers



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




what questions do you have for this code?

Here is the function call and output from your code:

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

123 + 49123 + 49

It should look like this:

  3801      123
-    2    +  49
------    -----