Arithmetic Formatter: Test Error

I finished my code for the Arithmetic Formatter, and it works fine when I try out different examples, but keeps giving me Error messages and I don‘t get why.
Here‘s my code:

def arithmetic_arranger(problems, x=False):
  if len(problems) > 5:
    print('Error: Too many problems.')
  nums = ''
  dens = ''
  sums = ''
  dashs = ''
  for k in problems:
    op = k.split()[1]
    if op == '+' or op == '-': num,den = k.split(op)
      print("Error: Operator must be '+' or '-'.")
    num = num.strip()
    den = den.strip()
    if len(num) > 4 or len(den) > 4:
      print('Error: Numbers cannot be more than four digits.')
    length = max(len(num), len(den)) + 2
    if num.isnumeric() == False or den.isnumeric() == False:
      print('Error: Numbers must only contain digits.')
    sump = int(num) + int(den)
    top = num.rjust(length)
    mid = op + ' ' + den.rjust(length - 2)
    nums += top + '    '
    dens += mid + '    '
    dashs += '-' * length + '    '
    sums += str(sump).rjust(length) + '    ' 
  if x == False:
    arranged_problems = nums + '\n' + dens + '\n' + dashs
  if x == True:
    arranged_problems = nums + '\n' + dens + '\n' + dashs + '\n' + sums
  return arranged_problems

Here‘s the link to the

What am I doing wrong?

Take a closer look at the challenge specification, function shouldn’t print anything on it own. Instead it should return the specified string, this includes situation when there’s error encountered.

1 Like

That‘s it, thank you!

I do understand the solution provided by Lukna. I am incorporting it as a solution to my challenge and i acknowledge his work. However,I am not able to get whats wrong in the above solution. Why is it giving error?
like what part is to be modified to get correct output.

It’s really best to create a new topic and provide a link to your code, as your issue is likely different than the original issue in this thread.

There are two more things wrong with the code, beside the print statements, as I later figured.

  1. The sum variable only uses addition, it needs an if statement to also be able to solve subtraction.

Something like this:

if op == '+':
    sol = int(num) + int(den)
    sol = int(num) - int(den)

(I changed the sum variable to sol)

  1. The final variables in the end need to be r-stripped. (Variable.rstrip())
1 Like