Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I have ran this code in Google Colab with all the test inputs and it correctly returned all the proper errors and operations, but when I run it in FreeCodeCamp, it doesn’t work at all. Is there some kind of formatting error or something? Thank you in advance!

``````def arithmetic_arranger(problems, show_answers=False):

def solver(problems):
results = []
for problem in problems:
if '-' in problem:
operands = problem.split('-')
result = int(operands[0]) - int(operands[1])
results.append(result)
else:
operands = problem.split('+')
result = int(operands[0]) + int(operands[1])
results.append(result)
return results

def formatting(problems, results):
formatted_problems = []
line1 = ''
line2 = ''
line3 = ''
line4 = ''

for i, problem in enumerate(problems):
operand1, operator, operand2 = problem.split()
operand1_len = len(operand1)
operand2_len = len(operand2)
max_length = max(operand1_len, operand2_len)

if '-' in problem:
result = results[i]
else:
result = results[i]

# Formatting for each line
line1 += operand1.rjust(2 + max_length) + '    '
line2 += operator + operand2.rjust(max_length + 1) + '    '
line3 += '-' * (max_length + 2) + '    '
line4 += str(result ).rjust(max_length + 2)+ '    '

# Formatting for each problem
formatted_problem = f"{operand1.rjust(2 + max_length)}\n{operator} {operand2.rjust(max_length)}\n{'-' * (max_length + 2)}\n{str(result)}\n"
formatted_problems.append(formatted_problem)

# Remove trailing spaces
line1 = line1.rstrip()
line2 = line2.rstrip()
line3 = line3.rstrip()
line4 = line4.rstrip()

return(line1 + "\n" + line2 + "\n" + line3 + "\n" + line4)
else:
return(line1 + "\n" + line2 + "\n" + line3)

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

valid_problems = r'^\d+\s*[-+]\s*\d+\$'
invalid_problems = r'[\*/]'

for problem in problems:
operand1, operator, operand2 = problem.split()
if len(operand1) > 4 or len(operand2) > 4:
return ('Error: Numbers cannot be more than four digits.')
elif not (operand1.isdigit() and operand2.isdigit()):
return ('Error: Numbers must only contain digits.')
elif re.match(valid_problems,problem) and not re.search(invalid_problems,problem):
continue
else:
return ("Error: Operator must be '+' or '-'.")

error = error_checker(problems)
if error:
return error
results = solver(problems)
return(formatting(problems, results))

print(arithmetic_arranger(problems, True))
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36`

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

It does not run because `problems` is not defined. Give it a proper value and you will see that the code will run. However, many tests are not satisfied.

Sorry for not clarifying, I have already tried this function with `problems` defined in google colab and it works, and i have defined it in FCC but it doesnt work in FCC. For example, I’ll call this function in colab:
`arithmetic_arranger(["3801 - 2", "123 + 49"])`
and it returns exactly what FCC asks for:
` 3801 123\n- 2 + 49\n------ -----`
(I literally just copy and pasted that result from both FCC and from colab results)
What could the issue be?

You didn’t import `re`. It should work after importing it.

wow, always the simple things with coding just finished it, thank you!!!

1 Like