Python Arithmetic Arranger confusion

Hi guys, so first off, I made the entire code in an IDE (because debugging is a lot easier) now when I paste the code on replit and try to run it, it throws up errors.

I understand all the conditions on which errors must be given by the program for example if the program is called with more than 5 problems it must give an error. My program displays the error message in this condition and then ends the program. But replit counts it as a failure (and I have no idea why). So, I’m not sure what kind of error I’m being expected to show.

before I show you the code, let me just say that I do know that the way I have declared lists is completely atrocious and I’ve also used a lot more variables than I needed to. I’m aware of the deficiencies, this is my first Python project so right now I’m just trying to complete it ASAP. Do give your critiques and tips but MOST IMPORTANTLY, plz tell me what replit is expecting from the program

code:

def arithmetic_arranger(problems, ans = False):

    error = 0

    #too many problems check
    error = tooManyProbs(problems, error)

    if error == 1: return "Error: Too many problems."

    prob = [["0" , "+" , "0"],["0" , "+" , "0"],["0" , "+" , "0"],["0" , "+" , "0"],["0" , "+" , "0"]]
    dashes = [0,0,0,0,0]
    answers = ['0','0','0','0','0']

    for i in range(len(problems)):
  
        #checking problem not empty and splitting it
        if problems[i] != "":
            prob[i] = problems[i].split()
            error = operatorDigitLenCheck(prob[i], error)

            if error == 1: return
    
    for i in range(len(problems)):    
        dashes[i] = calculateDashes(prob[i],dashes[i])

        #storing answers
        if ans == True:
            if prob[i][1] == "+":
                answers[i] = str(int(prob[i][0]) + int(prob[i][2]))
            else:
                answers[i] = str(int(prob[i][0]) - int(prob[i][2]))

    #printing numbers (and answers, if applicable)
    printing(prob, answers, dashes, ans)

    return




def tooManyProbs(problems, error):

    if len(problems) > 5:
        error = 1

    return error




def operatorDigitLenCheck(problem, error):
    #checking operator is either + or -
    if problem[1] != '+' and problem[1] != '-':
        print("Error: Operator must be '+' or '-'.")
        error = 1
        return error

    #checking numbers contain only digits
    if problem[0].isdigit() == False or problem[2].isdigit() == False:
        print("Error: Numbers must only contain digits.")
        error = 1
        return error

    #checking numbers don't exceed length of 4
    if len(problem[0]) > 4 or len(problem[2]) > 4:
        print("Error: Numbers cannot be more than four digits.")
        error = 1
        return error

    return error




def calculateDashes(problem, dashes):
    #finding out how many dashes to add

    if len(problem[0]) >=  len(problem[2]):
        dashes = 2 + len(problem[0])
    else:
        dashes = 2 + len(problem[2])

    return dashes




def printing(prob, answers, dashes, ans):
    
    emptySpaces = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

    for i in range(len(prob)):
        emptySpaces[i][0] = dashes[i] - len(prob[i][0])
        emptySpaces[i][1] = dashes[i] - len(prob[i][2]) - 1
        emptySpaces[i][2] = dashes[i] - len(answers[i])

    #print first line
    for i in range(len(prob)):
        if prob[i][0] != "0" and prob[i][2] != "0":
            for j in range(emptySpaces[i][0]):
                print(" ", end = "")
            print(prob[i][0], "    ", end = "")
    
    print("\n")
    #print second line
    for i in range(len(prob)):
        if prob[i][0] != "0" and prob[i][2] != "0":
            print(prob[i][1], end = "")
            for j in range(emptySpaces[i][1]):
                print(" ", end = "")
            print(prob[i][2], "    ", end = "")

    print("\n")
    #print dashes
    for i in range(len(dashes)):
        if dashes[i] != 0:
            for j in range(dashes[i]):
                print("-", end = "")
            print("    ", end = "")

    print("\n")
    #print answers
    if ans == True:
        for i in range(len(answers)):
            if prob[i][0] != "0" and prob[i][2] != "0":
                for j in range(emptySpaces[i][2]):
                    print(" ", end = "")
                print(answers[i], "    ", end = "")

    return

Challenge: Arithmetic Formatter

Link to the challenge:

First thing that jumps out is that the instructions are to return strings. You should never call print in your code.

It would help if you provide the output from the test suite.

1 Like

There should be an actual error message returned here.

You should not print anything. You must return a string.

You must return the error message as a string.

You must return the error message as a string.

You must return the error message as a string.

Return a string instead of printing.

1 Like

Thank you so much, Jeremy. Really appreciate the help!

1 Like

Sure thing. Feel free to ask more questions as they come up!

1 Like

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