Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
My code is not passing the tests but it is producing the results as described (I checked every case manually), I wrote my code in VS Code and pasted it to the arithmetic_formatter.py module.

Your code so far


separatedProblemList = []

def arithmetic_arranger(problem_list, optionalArgument = False):
    LineOne = ''
    LineTwo = ''
    LineThree = ''
    result = ''

    if len(problem_list) > 5 :
        return 'Error: Too many problems.' 
       

    for problem in problem_list :
        separatedProblemList.append(problem.split(' '))

    for separatedProblem in separatedProblemList:
        try: 
            operandOne = int(separatedProblem[0])
            operandTwo = int(separatedProblem[2])
        except:
            return 'Error: Numbers must only contain digits.'
        
        
        if len(str(operandOne)) > 4 or len(str(operandTwo)) > 4:
            return 'Error: Numbers cannot be more than four digits.'


        required_spaces = 1
        required_spaces += max(len(separatedProblem[0]), len(separatedProblem[2]))

        LineOne += ' ' + ' '*(required_spaces - len(separatedProblem[0])) + separatedProblem[0] + '    '
        LineTwo += separatedProblem[1] + ' '*(required_spaces - len(separatedProblem[2])) + separatedProblem[2] + '    '
        LineThree += '-' * required_spaces + '-' + '    '

        if optionalArgument == True:
            if separatedProblem[1] == '+':
                sum = operandOne + operandTwo
                result += ' ' + ' '*(required_spaces - len(str(sum))) + str(sum) + '    '
            elif separatedProblem[1] == '-':
                difference = operandOne - operandTwo
                result += ' ' + ' '*(required_spaces - len(str(difference))) + str(difference) + '    '
            else:
                return "Error: Operator must be '+' or '-'."
        

    LineOne = LineOne.rstrip()
    LineTwo = LineTwo.rstrip()
    LineThree = LineThree.rstrip()
    result = result.rstrip()

    LineOne += '\n'
    LineTwo += '\n'
 

    if optionalArgument == True:
        return LineOne + LineTwo + LineThree + '\n' + result

    return LineOne + LineTwo + LineThree

    

problem_list = ['1 + 2', '1 - 9380']
print(arithmetic_arranger(problem_list, True))

#print(separatedProblemList)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

This global variable abuse is a bad idea. I don’t see a reason why this needs to be out here in the global scope.


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Thanks for replying!!!
I will make sure I will use backticks whenever I will post next.

I made separatedProblemList = [] local, and it passed all the tests except one.

I was writing about that failed test, while writing I figured it out what was wrong, then I resubmitted the revised code and it passed all the tests.

Thanks Again !!!

Revised Code

def arithmetic_arranger(problem_list, optionalArgument = False):
    separatedProblemList = []
    LineOne = ''
    LineTwo = ''
    LineThree = ''
    result = ''

    if len(problem_list) > 5 :
        return 'Error: Too many problems.' 
       

    for problem in problem_list :
        separatedProblemList.append(problem.split(' '))

    for separatedProblem in separatedProblemList:
        try: 
            operandOne = int(separatedProblem[0])
            operandTwo = int(separatedProblem[2])
        except:
            return 'Error: Numbers must only contain digits.'
        
        
        if len(str(operandOne)) > 4 or len(str(operandTwo)) > 4:
            return 'Error: Numbers cannot be more than four digits.'


        required_spaces = 1
        required_spaces += max(len(separatedProblem[0]), len(separatedProblem[2]))

        LineOne += ' ' + ' '*(required_spaces - len(separatedProblem[0])) + separatedProblem[0] + '    '
        LineTwo += separatedProblem[1] + ' '*(required_spaces - len(separatedProblem[2])) + separatedProblem[2] + '    '
        LineThree += '-' * required_spaces + '-' + '    '

        if separatedProblem[1] == '+':
            sum = operandOne + operandTwo
            result += ' ' + ' '*(required_spaces - len(str(sum))) + str(sum) + '    '
        elif separatedProblem[1] == '-':
            difference = operandOne - operandTwo
            result += ' ' + ' '*(required_spaces - len(str(difference))) + str(difference) + '    '
        else:
            return 'Error: Operator must be \'+\' or \'-\'.'
        

    LineOne = LineOne.rstrip()
    LineTwo = LineTwo.rstrip()
    LineThree = LineThree.rstrip()
    result = result.rstrip()

    LineOne += '\n'
    LineTwo += '\n'
 

    if optionalArgument == True:
        return LineOne + LineTwo + LineThree + '\n' + result

    return LineOne + LineTwo + LineThree

    


1 Like

I have blurred the code solution you posted. I think because this is a project, you shouldn’t share your code here to avoid people copying it.

1 Like

Sorry, I will take care of that from now onwards. Thanks for blurring the code!

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