Arithmetic arranger testing issue

Hi,

I have been giving the arithmetic arranger challenge a go, but have run into an issue. The code I have made works when I test it in sublime, but when I try on repl it fails. I am struggling to understand what exactly is wrong. Could anyone give me a nudge in the right direction?

import re

def arithmetic_arranger(equations, answer):
	if len(equations)>4:
		print('Error: Too many problems')
		return
	'''setting up local variables'''
	numbers=[]
	allanswers=[]
	operators=[]
	dividingline='-----'''
	'''looping through the equations'''
	for e in equations:	
		'''checking if there are letters'''
		c=re.search('[a-zA-Z]', e)
		if c is not None:
			print('Error: Numbers must only contain digits.')
			return
		'''pulling the numbers out'''
		y=re.findall('[0-9]+', e)
		'''checking if each set of numbers is 4 digits maximum'''
		if len(y[0])>4:
			print('Error: Numbers cannot be more than four digits.')
			return
		if len(y[1])>4:
			print('Error: Numbers cannot be more than four digits.')
			return
		'''pulling the operator out'''
		z=re.findall('[+-]', e)
		'''checking if the operator is + or -, or if there are multiple operators'''
		if len(z) <1:
			print("Operator must be '+' or '-'")
			return
		if len(z) >1:
			print('Expressions can only have one operator')
			return
		'''setting the calculation to + or -'''
		if z[0]=='+':
			operator=1
		if z[0]=='-':
			operator=2
		'''performing the calculation'''
		if operator==1:
			equationanswer=int(y[0])+int(y[1])
			stringanswer=str(equationanswer)
		if operator==2:
			equationanswer=int(y[0])-int(y[1])
			stringanswer=str(equationanswer)
		'''saving all the results'''
		numbers.append(y[0])
		numbers.append(y[1])
		allanswers.append(stringanswer)
		operators.append(z[0])
	
	'''printing results'''
	print(f'{numbers[0].rjust(6)}    {numbers[2].rjust(6)}    {numbers[4].rjust(6)}    {numbers[6].rjust(6)}')
	print(f'{operators[0]} {numbers[1].rjust(4)}   {operators[1]} {numbers[3].rjust(5)}    {operators[2]} {numbers[5].rjust(4)}    {operators[3]} {numbers[7].rjust(4)}')
	print(f'{dividingline.rjust(6)}    {dividingline.rjust(6)}    {dividingline.rjust(6)}    {dividingline.rjust(6)}')
	'''checking if answers should be shown, and printing'''
	if answer==True:
		print(f'{allanswers[0].rjust(6)}    {allanswers[1].rjust(6)}    {allanswers[2].rjust(6)}    {allanswers[3].rjust(6)}')
	

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)

your function needs to output the strings using the return statement

Thanks, that makes sense. Unfortunately there is obviously something I am missing about return statements.

I rearranged the code to save the string as the variable arranged_problems.

	arranged_problems=f"{numbers[0].rjust(6)}    {numbers[2].rjust(6)}    {numbers[4].rjust(6)}    {numbers[6].rjust(6)}\n{operators[0]} {numbers[1].rjust(4)}   {operators[1]} {numbers[3].rjust(5)}    {operators[2]} {numbers[5].rjust(4)}    {operators[3]} {numbers[7].rjust(4)} \n{dividingline.rjust(6)}    {dividingline.rjust(6)}    {dividingline.rjust(6)}    {dividingline.rjust(6)}"
	return arranged_problems

print(arithmetic_arranger(["98 + 375", "3801 - 2", "45 + 43", "123 + 49", "5+9"]))

However, this returns “None”. If instead I set arranged_problems as a global variable, have the function use the global version and then print the global version after the function was called I get the correctly formatted version.

arithmetic_arranger(["98 + 375", "3801 - 2", "45 + 43", "123 + 49"])
print(arranged_problems)

98      3801        45       123
  • 375 - 2 + 43 + 49

[Finished in 0.3s]
(when running the file it ended correctly aligned but copying it here changed it)

I am sure I am missing something obvious, I just can’t quite find it. Any help would be greatly appreciated.

Thank you

if you give the link to your repl and/or give the full code, it’s much easier to debug

right now I would just play the guessing game in figuring out what’s the issue