Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I have completed my code. I checked and it is providing what “Should Return” is asking. But, it does not pass the Test. How do I find out where it is wrong.

Thank you in an advance for reading this post.

SS

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    number_of_problem = len(problems)
    
    if  number_of_problem > 5:
        print('Error: Too many problems.')
        return
    
    num1_list = []
    num2_list = []
    operator_list = []
    max_len_list = []
    result_list = []
    
    for i in problems:
        num1, operator, num2 = i.split()
        num1_list.append(num1)
        operator_list.append(operator)
        num2_list.append(num2)
        max_len = max(len(str(num1)), len(str(num2)))
        max_len_list.append(max_len)
        

    for i in operator_list:
        if i != "-" and i != "+":
            print("Error: Operator must be '+' or '-'.")
            return
    
    for i in num1_list:
        digits = len(i)
        if digits > 4:
            print('Error: Numbers cannot be more than four digits.')  
            return 
        
    for i in num2_list:
        digits = len(i)
        if digits > 4:
            print('Error: Numbers cannot be more than four digits.')
            return
        
    for i in problems:
        num1, operator, num2 = i.split()
        try: 
            result_list.append(eval(i))            
        except:
            print('Error: Numbers must only contain digits.')
            return
            
    first_row_string = ""
    second_row_string = ""
    third_row_string = ""
    fourth_row_string = ""
    tab = " " * 4
    for i in range(number_of_problem):
        prefix = operator_list[i] + ' '
        
        #first row creation
        space_num1 = len(prefix) + max_len_list[i] - len(num1_list[i])
        first_row_string += " "*space_num1 + str(num1_list[i]) + tab
        
        #second row creation
        space_num2 = max_len_list[i] - len(num2_list[i])
        second_row_string += prefix + ' '*space_num2 + str(num2_list[i]) + tab

        #third row creation
        dash = max_len_list[i] + len(prefix)
        third_row_string += '-'*dash + tab 

        #fourth row creation
        space_result = len(prefix) + max_len_list[i] - len(str(result_list[i]))
        fourth_row_string += ' '*space_result + str(result_list[i]) + tab
    
    if show_answers:
        print(first_row_string[:-4] +'\n' + second_row_string[:-4] + '\n' + third_row_string[:-4] +'\n' + fourth_row_string[:-4])
    else:
        print(first_row_string[:-4] +'\n' + second_row_string[:-4] + '\n' + third_row_string[:-4])

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

your function always return None, please return a string instead

1 Like

never mind I solved my own problem. I just needed to return instead of print(‘error’)

yep, I was able to solve it when you posted! Now to next problem!