Python Project - Arithmetic Formatter

Hi Guys, I have written a code which seems to work fine for me but it doesn’t pass 5 tests. I am unable to understand the error messages. I am not looking for solution, just a better understanding of the error message so that I can work on solution. If you have any idea on better organization of my codes, You can convey that as well.

Here is my code.

def arithmetic_arranger(problems, calc=False):  
#  
    operands_list = []
    operators_list = []
    calculated_list = [] 

    if len(problems) >= 5:
        return "Error: Too many problems."
# 
    for problem in problems:
        problem_input = problem.split()
        operands_list.extend([problem_input[0], problem_input[2]])
        operators_list.extend([problem_input[1]])  
# 
    for operand in operands_list:
        if len(operand) > 4:
            return "Error: Numbers cannot be more than four digits."
        if not operand.isnumeric():
            return 'Error: Numbers must only contain digits.'
# 
    for operator in operators_list:
        if not (operator == '+' or operator == "-"):
            return "Error: Operator must be '+' or '-'."

# ALL ERRORS TAKEN CARE OF. TIME TO EVALUATE AND ARRANGE.
    for problem in problems:
        if calc == True:
            calculated_list.extend([eval(problem)])

    first_line = ''
    second_line = ''
    dashed_line = ''
    calculated_line = ""

    for i in range(len(operands_list)):
        if i % 2 == 0:
            # 2 has been added as actual length has one operator and a space extra
            bigger_len = len(operands_list[i]) + 2 if len(operands_list[i]) > len(operands_list[i + 1]) else len(operands_list[i + 1]) + 2 
            diff = len(operands_list[i]) - len(operands_list[i + 1])
            # TypeError: Str + int & index out of range  
            calculated_line += (bigger_len - len(str(calculated_list[i//2]))) * " " + str(calculated_list[i//2]) + " " * 4 if calc else ""
            # print(i)
           
            if diff < 0:
                first_line += " " * (abs(diff) + 2) + operands_list[i] + " " * 4
                second_line += operators_list[i // 2 ] + " " + operands_list[i + 1] + " " * 4
                dashed_line += bigger_len * '-' + " " * 4 
                
            else:
                first_line += " " * 2 +  operands_list[i] + " " * 4
                second_line += operators_list[i // 2] +  " " * (abs(diff) + 1) + operands_list[i + 1] + " " * 4
                dashed_line += bigger_len * '-' + " " * 4
                
    
    # print(first_line)
    # print(second_line)
    # print(dashed_line)
    # print(calculated_line)
    arranged_problems = first_line.rstrip() + '\n' + second_line.rstrip() + '\n' + dashed_line.strip() + '\n' + calculated_line.rstrip() 
    return arranged_problems



print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
print(arithmetic_arranger(["3801 - 2", "123 + 49"], True))
print(arithmetic_arranger(["1 + 2", "1 - 9380"], True))
print(arithmetic_arranger(['3 + 855', '3801 - 2', '45 + 43', '123 + 49'], True))





Thank you for your time.

I just figured out from error messages that I was adding a new line at the end of dashed_line even when calc=False leading to the errors. After taking care of it, I just got 8 test to pass while earlier it was only 5. So, I guess I am making progress.

Here is the updated code:

def arithmetic_arranger(problems, calc=False):  
#  
    operands_list = []
    operators_list = []
    calculated_list = [] 

    if len(problems) >= 5:
        return "Error: Too many problems."
# 
    for problem in problems:
        problem_input = problem.split()
        operands_list.extend([problem_input[0], problem_input[2]])
        operators_list.extend([problem_input[1]])  
# 
    for operand in operands_list:
        if len(operand) > 4:
            return "Error: Numbers cannot be more than four digits."
        if not operand.isnumeric():
            return 'Error: Numbers must only contain digits.'
# 
    for operator in operators_list:
        if not (operator == '+' or operator == "-"):
            return "Error: Operator must be '+' or '-'."

# ALL ERRORS TAKEN CARE OF. TIME TO EVALUATE AND ARRANGE.
    for problem in problems:
        if calc == True:
            calculated_list.extend([eval(problem)])

    first_line = ''
    second_line = ''
    dashed_line = ''
    calculated_line = ""

    for i in range(len(operands_list)):
        if i % 2 == 0:
            # 2 has been added as actual length has one operator and a space extra
            bigger_len = len(operands_list[i]) + 2 if len(operands_list[i]) > len(operands_list[i + 1]) else len(operands_list[i + 1]) + 2 
            diff = len(operands_list[i]) - len(operands_list[i + 1])
            # TypeError: Str + int & index out of range  
            calculated_line += (bigger_len - len(str(calculated_list[i//2]))) * " " + str(calculated_list[i//2]) + " " * 4 if calc else ""
            # print(i)
           
            if diff < 0:
                first_line += " " * (abs(diff) + 2) + operands_list[i] + " " * 4
                second_line += operators_list[i // 2 ] + " " + operands_list[i + 1] + " " * 4
                dashed_line += bigger_len * '-' + " " * 4 
                
            else:
                first_line += " " * 2 +  operands_list[i] + " " * 4
                second_line += operators_list[i // 2] +  " " * (abs(diff) + 1) + operands_list[i + 1] + " " * 4
                dashed_line += bigger_len * '-' + " " * 4
                
    
    # print(first_line)
    # print(second_line)
    # print(dashed_line)
    # print(calculated_line)
    if calc:
        arranged_problems = first_line.rstrip() + '\n' + second_line.rstrip() + '\n' + dashed_line.strip() + '\n' + calculated_line.rstrip() 
    else:
        arranged_problems = first_line.rstrip() + '\n' + second_line.rstrip() + '\n' + dashed_line.strip()
    return arranged_problems



print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
print(arithmetic_arranger(["3801 - 2", "123 + 49"], True))
print(arithmetic_arranger(["1 + 2", "1 - 9380"], True))
print(arithmetic_arranger(['3 + 855', '3801 - 2', '45 + 43', '123 + 49'], True))




Ah!!! The limit is 5. Anything more should lead to the error. Not the 5. Upto 5, it should not throw error. This was one of the issues with my code. A logical Error. It should have been,

    if len(problems) > 5:
        return "Error: Too many problems."

without that equal sign. I am sorry. I think I can figure out the last one as well. I have learnt that one should not give up. Maybe one is just one attempt away from success. The moment I posted here and went back to reading the error messages, I started understanding it. Gotta solve the last test I am failing.

All test has passed. This thread should be considered closed.