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: