How to not return None? (Arithmetic Formatter)

I’ve managed to get the function to work perfectly when I submit a list via Print().

My problem is when I’m running the actual test_module.py

The problem seems to be with my return statements. Initially, I set the entire thing up so that I return a print statement of the errors. That was an issue as it didn’t return an argument of any kind. Causing the actual tests to see an error from the Print statement and then None a line under.

I’ve changed the first few statements to not behave this way. So I’m printing the error and then returning arranged_problems. The problem with that is that the error is printed and then the list is printed.

What’s the best approach to this? Everything works. But I know there is an issue with the way I’m handling return statements.

Here’s my code:

def arithmetic_arranger(problems, second=False):
  arranged_problems = problems
  
  top_row = []
  operator = []
  bottom_row = []
  solutions_row = []
  problem_display_length = []

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

  arranged_problems = ''

  #split to rows
  for problem in problems:
    items = problem.split()

    if len(items[0]) > 4 or len(items[2]) > 4:
      print('Error: Numbers cannot be more than four digits.', end='')
      return arranged_problems
    else:
      top_row.append(items[0])
      bottom_row.append(items[2])

    if items[1] != '+' and items[1] != '-':
      print("Error: Operator must be '+' or '-'.", end='')
      return arranged_problems

    operator.append(items[1])

  for item in top_row:
    try:
      int(item)
    except:
      return print(" Error: Numbers must only contain digits. ")

  for item in bottom_row:
    try:
      int(item)
    except:
      return print(" Error: Numbers must only contain digits. ")

  #determine length of each problem and run calculations
  for x, problem in enumerate(problems):
    problem_display_length.append(
      len(str(max(int(top_row[x]), int(bottom_row[x])))) + 2
      )#which number is longer in each row, then capture length + 2 for operator and space

    if operator[x] == '+': 
      solutions_row.append( str(int(top_row[x]) + int(bottom_row[x])) )

    if operator[x] == '-': 
      solutions_row.append( str(int(top_row[x]) - int(bottom_row[x])) )
    

  #construct top row
  for x, problem in enumerate(problems):
    arranged_problems += (' ' * ((problem_display_length[x]) - len(str(top_row[x]))) + top_row[x] + '    ')
    
  arranged_problems += '\n' 

  #construct middle row
  #for x, problem in enumerate(problems):
  #  arranged_problems += (' ' * ((problem_display_length[x]) - len(operator[x])) + operator[x] + '   ')
  # arranged_problems += '\n' 

  #construct bottom row
  for x, problem in enumerate(problems):
    arranged_problems += (operator[x] + ' ' * ((problem_display_length[x]) - len(bottom_row[x]) - 1) + bottom_row[x] + '    ')

  arranged_problems += '\n' 

  #construct 4 spaces
  for x, problem in enumerate(problems):
    arranged_problems += ('-' * problem_display_length[x] + '    ')

  arranged_problems += '\n'

  if second is True:
    #construct row of solutions
    for x, problem in enumerate(problems):
      arranged_problems += (' ' * ((problem_display_length[x]) - len(solutions_row[x])) + solutions_row[x] + '    ')

    #construct bottom row

  return arranged_problems

Here’s a link to my code:

https://repl.it/join/gprgvlwg-edanbenatar

Function isn’t expected to print anything on it own, but in every case return appropriate string. The None comes from fact that print function returns it.
In line like:

return print(" Error: Numbers must only contain digits. ")

At first print prints out the text, then it returns None, which is later returned further by the return.

1 Like

That’s my issue though. None is interpreted as the last item displayed and not the actual Error message.

Then I face issues like this one:

AssertionError: None != 'Error: Too many problems.' : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

None is what your function return. It happens if you do return print()

delete all print functions (other than if you need them for debugging), and return the required values, not print them

1 Like

Yes, because the string in print is printed instead of returned. Calling print function there is not required or wanted to. Take a look how example functions below behave.

def func():
    return print('text')

def func2():
    return 'text'

func prints out to console ‘text’, but it returns None, on the other hand func2 doesn’t print out anything, but it returns text. From arithmetic_arranger function the second one is expected in every case.

1 Like

Thank you all so much!
The elimination of actual print function did it!

It also unraveled for me a few other display errors that I fixed.

Project complete! :white_check_mark: