Arithmetic arranger problem with returning a string

Tell us what’s happening:
I’ve been having problems with returning a string instead of just printing out the formatted calculation thing. I’ve managed to ‘solve’ the problem and print out the numbers correctly formated but it obviously doesn’t return as a string and i have no idea how to do that.
Can i even do something like (" " * x) on a string like i can on a print statement? Which is something i’ve used, this is getting frustrating because it’s such a dumb issue :frowning:
x, y and z are basically the number of spaces i have to print out, it’s probably not obvious to someone reading the code

Your code so far

def arithmetic_arranger(problems):

  # error checking before any calculations are made, starting by the number of problems provided
  if len(problems) > 5:
    return 'Error: Too many problems.'
  
  # checking for more errors
  for value in problems:
    # number of digits and operator error checking
    if len(value.split()[0]) > 4 or len(value.split()[2]) > 4:
      return 'Error: Numbers cannot be more than four digits.'
    elif value.split()[1] not in ['+', '-']:
      return "Error: Operator must be '+' or '-'."
    
    # now this actually works, didn't expect it to. I don't think it's the correct way to do it though, however it's there and it works. checking if the number is actually a number
    try:
      test_var1 = int(value.split()[0])
      test_var2 = int(value.split()[2])
    except:
      return "Error: Numbers must only contain digits."
  
  # actually writing the function now
  first_number = list()
  second_number = list()
  operators = list()

  # creating lists
  for value in problems:
    first_number.append(value.split()[0])
    second_number.append(value.split()[2])
    operators.append(value.split()[1])
    print("first", first_number, "operator", operators, "second", second_number)

  for j in range(3):
    for i in range(len(problems)):
      flen = len(first_number[i])
      slen = len(second_number[i])
      
      if flen > slen:
        y = 2
        x = 1 + flen - slen
        z = 2 + flen
      elif slen > flen:
        x = 1
        y = 2 + slen - flen
        z = slen + 2  
      elif slen == flen:
        x = 1
        y = 2
        z = flen + 2
      
      if j == 0:
        #print("first line")
        print(" " * y, first_number[i], " " * 4,sep="", end="")
      elif j == 1:
        if i == 0:
          print()
        print(operators[i], " " * x, second_number[i], " " * 4, sep = "", end="")
      elif j == 2:
        if i == 0:
          print()
        print("-" * z, " " * 4, end="", sep="")
  return

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

1 Like

Maybe this could help

sure, it’s the same thing because you are building a string inside the print statement

to get

H   B
E   O
L   S
L   S
O

you need to write

H   B\nE   O\nL   S\nL   S\nO