Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I have completed the code for the python arithmetic formatter project, but all the tests failed. I tried the individual arranger problems and get the desired output but still failed the test. I initially thought that it was because I had additional space " " behind each line of code and so used .rstrip() to remove it before compiling the lines, but the tests are still unsuccessful. Is my coding logic at fault ir should I employ different built-in functions?

Your code so far

def arithmetic_arranger(problems, solve=False): 
    
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""
    max_count = 0
    numprob = 0
    digitprob = 0       #return 'Error: Numbers cannot be more than four digits.'
    arithprob = 0       #print("Error: Operator must be '+' or '-'.")
    numericprob = 0     #return 'Error: Numbers must only contain digits.'

    for problem in problems:
        individual_problem = problem.split()
        numprob += 1

        # check for any number > than 4 digits
        for seperate in individual_problem:    
            if len(seperate) > 4:
                digitprob += 1
        
        # arithmetic check for '+' and '-'
        if individual_problem[1] != '+' and individual_problem[1] != '-':
            arithprob += 1

        # check if provided numbers are numeric
        if individual_problem[0].isnumeric() and individual_problem[2].isnumeric():
            numericprob -= 1
        else:
            #numericprob += 1
            print("Error: Numbers must only contain digits.")
            break
        
        # check for max len of 2 number given in each calculation
        if len(individual_problem[0]) > len(individual_problem[2]):
            max_count = len(individual_problem[0])
        else:
            max_count = len(individual_problem[2])

        total = 0
        lentotal = 0

        line1 += (((max_count+2)-len(individual_problem[0]))*' ') + individual_problem[0] + (4*' ')
        line2 += individual_problem[1] + (((max_count+1)-len(individual_problem[2]))*' ') + individual_problem[2] + (4*' ')
        line3 += ((max_count + 2)*'-') + (4*' ')

        if individual_problem[1] == '+':
            total = int(individual_problem[0]) + int(individual_problem[2])
        elif individual_problem[1] == '-':
            total = int(individual_problem[0]) - int(individual_problem[2])

        lentotal = len(str(total))
        line4 += (((max_count - lentotal)+2)*' ') + str(total) + (4*' ')

    if numprob > 5:   
        print("Error: Too many problems.")

    elif digitprob > 0:
        print("Error: Numbers cannot be more than four digits.")

    elif arithprob > 0:
        print("Error: Operator must be '+' or '-'.")

    else:
        if solve == True:
            print(line1.rstrip() + '\n' + line2.rstrip() + '\n' + line3.rstrip() + '\n' + line4.rstrip())
         
        else:
            print(line1.rstrip() + '\n' + line2.rstrip() + '\n' + line3.rstrip())
    return 

#arithmetic_arranger(["3801 - 2", "123 + 49"])
#arithmetic_arranger(["1 + 2", "1 - 9380"])
#arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
#arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
#arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
#arithmetic_arranger(["3 + 855", "988 + 40"], True)
#arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Function is not expected to print anything on it own, but to return string with arranged problems, or specified error.

1 Like

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