Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Hi, guys!
When I run my code on Replit it fails on 6 tests and it seems to be because of the formatting.
But I’m not finding any indentation error or wrong spaces between the elements.
Could you guys, please, help me?

Your code so far

    # Checking the lenght of the problems
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        firstLine = ''
        secondLine= ''
        dashes = ''
        results = ''
        for operation in range(0, len(problems)):
            [firstItem, operator, secondItem] = problems[operation].split(" ")
            # Checking if the numbers has the max of 4 digits
            if len(firstItem) > 4 or len(secondItem) > 4:
                return "Error: Numbers cannot be more than four digits."
            # Checking if the numbers are numerics
            elif firstItem.isnumeric() == False or secondItem.isnumeric() == False:
                return "Error: Numbers must only contain digits."
            # Checking if the operator is equal to '+' or '-'
            elif operator != '+' and operator != '-':
                return "Error: Operator must be '+' or '-'."
            # Adding the spaces
            # If the length of the first and second operand are equal
            if len(firstItem) == len(secondItem):
                firstItem = firstItem.rjust(len(firstItem) + 2, " ")
                secondItem = secondItem.rjust(len(secondItem) + 1, " ")
            # If the first operand is greatter than the second operand
            elif len(firstItem) > len(secondItem):
                space = len(secondItem) + 1 + (len(firstItem) - len(secondItem))
                secondItem = secondItem.rjust(space, " ")
                firstItem = firstItem.rjust(len(firstItem) + 2, " ")
            # If the second operand is greatter than the first operand
            elif len(secondItem) > len(firstItem):
                space = len(firstItem) + 2 + (len(secondItem) - len(firstItem))
                firstItem = firstItem.rjust(space, " ")
                secondItem = secondItem.rjust(len(secondItem) + 1, " ")
            # Creating the dashes line
            dash = ''
            while len(dash) != len(firstItem):
                dash = dash + "_"
            # Calculating the result
            if operator == "+":
                result = str(int(firstItem) + int(secondItem))
            elif operator == "-":
                result = str(int(firstItem) - int(secondItem))
            # Adding the spaces to the result
            space = len(result) + (len(firstItem) - len(result))
            result = result.rjust(space, " ")
            secondItem = operator + secondItem
            # Adding to the strings
            if (operation + 1) != len(problems):
                firstLine += firstItem  + "    "
                secondLine += secondItem  + "    "
                dashes += dash + "    "
                results += result + "    "
            else: 
                firstLine += firstItem 
                secondLine += secondItem 
                dashes += dash
                results += result
        # Returning the result
        firstLine.rstrip()
        secondLine.rstrip()
        dashes.rstrip()
        results.rstrip()
        if showResult==False:
            finalResult = firstLine + "\n" + secondLine + "\n" + dashes
        elif showResult==True:
            finalResult = firstLine + "\n" + secondLine + "\n" + dashes + "\n" + results
    return finalResult

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Thank you, Randell Dawson!!
It worked! :grinning: :grinning: :grinning: :grinning:

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