Arithmetic Formatter

Hi,
I am working on the first task in scientific computing with python: arithmetic formatter
( Scientific Computing with Python Projects - Arithmetic Formatter | Learn | freeCodeCamp.org

I am wondering why my code is not working if I run it in Replit
I am just starting to code, so it would be great to hear about improvements :slight_smile:

Cosi

def arithmetic_arranger(problems, result = False):

    # divide the calculation
    firstline = ""
    secondline = ""
    dashline = ""
    resultline = ""

    for problem in problems:
        number1 = problem.split()[0]
        operator = problem.split()[1]
        number2 = problem.split()[2]
        #print(number1)
        #print(number2)

        # check operator
        if not (operator == "+" or operator == "-"):
            return("Error: Operator must be '+' or '-'.")

        # check if digit
        if not (number1.isdigit or number2.isdigit):
            return("Error: Numbers must only contain digits.")

        # length of numbers
        if (len(number1) > 4 or len(number2) > 4):
            return("Error: Numbers cannot be more than four digits.")

        # find longest_number
        number_list = [number1, number2]
        longest_word = max(number_list, key=len)
        i = len(longest_word)
        #print(i)

        # alignment
        upper_line = number1.rjust(i + 2)
        bottom_line = operator + number2.rjust(i + 1)
        #print(upper_line)
        #print(bottom_line)

        # if result = true
        if result == True:
            if operator == "+":
                sum = str(int(number1) + int(number2))


            else:
                sum = str(int(number1) - int(number2))


        sum = sum.rjust(i + 2)
        dash = "-" * (i)
        dash = dash.rjust(i + 2)
            #print(dash)
            #print(result)
            #print("\n")



        firstline = firstline + upper_line + "    "
        secondline = secondline + bottom_line + "    "
        dashline = dashline + dash + "    "
        resultline = resultline + sum + "    "


    print(firstline)
    print(secondline)
    print(dashline)
    print(resultline)

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

hello and welcome to this forum!

It would be great to get a link to your repl.it so we can see the situation live (including the errors etc).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.