Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I wrote these codes that meet the test’s output requirements, but , it not pass by .. could anybody help or give hint to sovle ?
Thanks a lot !

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    # changed_problems = problems
    # problems =''
    if len(problems) > 5:
        raise ValueError("Error: Too many problems.")
        
    start_nums =[]
    operators = []
    end_nums = []
    results = []
    dashes = []
   
    for problem in problems:

        start_num, operator , end_num = problem.split()
        
        if start_num.isalpha() or end_num.isalpha():
            raise TypeError('Error: Numbers must only contain digits.')
            break
        elif len(start_num) > 4 or len(end_num)>4:
            raise ValueError('Error: Numbers cannot be more than four digits.')
            break

        elif operator not in['+','-']:
            raise TypeError("Error: Operator must be \'+\' or \'-'.")
            break
        else:
            if operator == "+":
                results.append(str(int(start_num) + int(end_num)))
            if operator == "-": 
                results.append(str(int(start_num) - int(end_num)))

            start_nums.append(str(start_num))
            operators.append(operator)
            end_nums.append(str(end_num))

    for i in range(len(operators)):
        a = 0
        end_nums[i] = operators[i]+' '+end_nums[i]
        a = max(len(start_nums[i]),len(end_nums[i]),len(results[i]))        
        start_nums[i] = ""+(a - len(start_nums[i]))*" "+ start_nums[i]+"\t"
        end_nums[i] = (a - len(end_nums[i]))*" "+ end_nums[i]+"\t"
        results[i] = (a - len(results[i]))*" "+results[i]+"\t"
        dashes.append("-"*a+"\t")
    
    start_nums = " ".join(start_nums).strip(' ')
    end_nums = " ".join(end_nums).strip(' ')
    dashes = " ".join(dashes).strip(' ')
    results = ' '.join(results).strip(' ')
   
    problems =[problem for problem in [start_nums,end_nums,dashes,results]]
   

    if show_answers:
        for i in range(len(problems)):
            print(f'{problems[i]}')
        
    problems = ''
    return problems

print(f'\n{arithmetic_arranger(["9999 - 9999","32 + 8", "1 - 3801", "9999 - 9999", "523 - 49"],True)}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

read the requirement for when you have an invalid input

arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers must only contain digits.' .

is your function returning the requested value?

you have the same issue everywhere, your function is never returning the requested value, it’s raising an error or printing

1 Like

Okay . Thanks so much .. My English is terrible .. I misunderstood “return” with “raise” ..