Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I think I already fullfill the request of project, but the resualt on freecodecamp console is differ from spyder

Your code so far

import re


def arithmetic_arranger(problems, show_answers=False):
    
    pattern = re.compile(r'(\d+)\s([-+*/])\s(\d+)')
    digit_pattern1 = re.compile(r'\D+\s([-+*/])\s')
    digit_pattern2 = re.compile(r'\d+\D+\s([-+*/])\s')
    digit_pattern3 = re.compile(r'\D+\d+\s([-+*/])\s')
    digit_pattern4 = re.compile(r'\d+\s([-+*/])\s\D+')
    digit_pattern5 = re.compile(r'\d+\s([-+*/])\s\d+\D+')
    number_listA =[]
    number_listB =[]
    operator_list=[]
    resualt_list=[]
    #check problem limit
    if len(problems)>5:
       return print('Error: Too many problems.')
      
    #creat problem list    
    for problem in (problems):
        #check digits
        if digit_pattern1.findall(problem):
            return print('Error: Numbers must only contain digits.')
        elif digit_pattern2.findall(problem):
            return print('Error: Numbers must only contain digits.')
        elif digit_pattern3.findall(problem):
            return print('Error: Numbers must only contain digits.')
        elif digit_pattern4.findall(problem):
            return print('Error: Numbers must only contain digits.')
        elif digit_pattern5.findall(problem):
            return print('Error: Numbers must only contain digits.')
        
        matches = pattern.findall(problem)
        for match in(matches):
            numA = match[0]
            operator = match[1]
            numB = match[2]
            #check operator without * or /
            if match[1]=='*' or match[1]=='/':
                return print("Error: Operator must be '+' or '-'.")
            #check only 4 digits
            if len(numA)>4 or len(numB)>4:
                return print('Error: Numbers cannot be more than four digits.')
        
            
            number_listA.append(numA)
            operator_list.append(operator)
            number_listB.append(numB)
            if operator =='+':
                resualt_list.append(int(numA)+int(numB))
            else:
                resualt_list.append(int(numA)-int(numB))
    #print answer
    arranged_problems = ""

    # arrange problems
    for i in range(len(problems)):
        width = max(len(number_listA[i]), len(number_listB[i])) + 2
        arranged_problems += f"{number_listA[i]:>{width}}    "

    arranged_problems += "\n"

    for i in range(len(problems)):
        width = max(len(number_listA[i]), len(number_listB[i]))
        arranged_problems += f"{operator_list[i]} {number_listB[i]:>{width}}    "

    arranged_problems += "\n"

    for i in range(len(problems)):
        width = max(len(number_listA[i]), len(number_listB[i]))+2
        arranged_problems += f"{'-' * (width)}    "

    arranged_problems += "\n"
    
    if show_answers:
        for i in range(len(problems)):
            width = max(len(number_listA[i]), len(number_listB[i]))+2
            arranged_problems += f"{resualt_list[i]:>{width}}    "
    return print(arranged_problems)
arithmetic_arranger(["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/122.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Which tests are failing? I suspect that you have an issue when the solutions aren’t supposed to be shown. Also you shouldn’t have extra whitespace at the end of lines.

You can use repr to show raw string output

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

Then you can clearly see your output and there’s an extra \n at the end

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

None of the tests will work like this:

return print(arranged_problems)

you need to return arranged_problems only

Thanks for your remind of extra white space at the end of lines, and the return description.
I was too concerned about the output showen on the console.

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