Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Well first 4 and last 2 tests are failed but when im checking the return of my code its the sames as in the test any clue ?

Your code so far

#importing regular expresion for spliting list elements
import re
def arithmetic_arranger(problems, show_answers=False):
    #Checking if there is not to many problems (max 5)
    if len(problems) >= 5:
        return ('Error: Too many problems.')
    else:
        first_part = []
        second_part = []
        third_part = []
        results = []
        #spliting list elements
        for n in problems:
            result = 0
            parts = re.split(r' ', n)
            str1 = parts[0].strip()
            #Checking if first element can be int (if not raise error)
            try:
                num1 = int(parts[0].strip())
            except:
                return ('Error: Numbers must only contain digits.')
            operator = parts[1].strip()
            
            str2 = parts[2].strip()
            #Checking if third element can be int (if not raise error)
            try:
                num2 = int(parts[2].strip())
            except:
                return ('Error: Numbers must only contain digits.')
            #Append str type elements to new lists soo i can print it later
            first_part.append(str1)
            second_part.append(operator)
            third_part.append(str2)
            #Checking if operort is + or - if not raise error
            if operator not in ['+', '-']:
                return ("Error: Operator must be '+' or '-'.")
            else:
                #Numbers can have max 4 digits thats how i checked that
                if len(str1) <= 4 and len(str2) <= 4:
                    #Addition and subtraction 
                    if operator == '+':
                        result = num1 + num2
                        results.append(str(result))
                    
                    else:
                        result = num1 - num2
                        results.append(str(result))
                else:                   
                #Error if there is more then 4 digits
                    return ('Error: Numbers cannot be more than four digits.')
        #Strings that will hold elements together
        first_line = ''
        second_line = ''
        third_line = ''
        dash_line = ''
        #loop to connect strings
        for i in range(len(problems)):
            max_length = max(len(first_part[i]),len(third_part[i])) + 2
            first_line += first_part[i].rjust(max_length) + '    '
            second_line += second_part[i] + third_part[i].rjust(max_length - 1) + '    '
            dash_line += '-' * max_length + '    '
            if show_answers:
                third_line += results[i].rjust(max_length) + '    '
        #checking if show_answer is true or not
        if show_answers:
            show_problems = f"{first_line}\n{second_line}\n{dash_line}\n{third_line}"
        else:
            show_problems = f"{first_line}\n{second_line}\n{dash_line}"
        print (show_problems)
        return show_problems

arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

The instructions mention that you should look at the browser console (usually opened with F12 key) so you can see the detailed test logs. Have you checked? Please include a copy of the console logs here if you need help understanding them.

Use print() and repr() to call your function and it will return in the same format as the tests show, with visible spaces and newline characters. For example:

print(repr(arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])))

It will be easier to compare.

arithmetic_arranger(["3801 - 2", "123 + 49"]) should return ' 3801 123\n- 2 + 49\n------ -----'.

print(
    repr(
    arithmetic_arranger(["3801 - 2", "123 + 49"]) 
    )
)

>>> '  3801      123    \n-    2    +  49    \n------    -----    '

Compare the test and your result:

'  3801      123\n-    2    +  49\n------    -----'
'  3801      123    \n-    2    +  49    \n------    -----    '

You can see they don’t line up. You have extra spaces before each newline and at the end.

1 Like

Thanks a lot :slight_smile: got it now

1 Like