Arithmetic Formatter Formatting Problems

Hi!

I am having a few formatting problems with my Arithmetic Formatter. I have passed 4 of the 6 tests so far. My problems are that in both output tests, the first operand is two spaces to the right of where they are supposed to be. Below is the code I have so far.

def arithmetic_arranger(problems, statprint = False):

Limit of 5 Arithmetic Problems

if len(problems) > 5:
return ‘Error: Too many problems.’

Initialize the output lines.

line_1 = ‘’
line_2 = ‘’
line_3 = ‘’
line_4 = ‘’

Parse the problem strings.

for i, problem in enumerate(problems):
first, operator, second = problem.split()
length_1 = len(first)
length_2 = len(second)

Operations are limited to addition and subtraction.

if operator not in ['+', '-']:
  return "Error: Operator must be '+' or '-'."

Operands must only contain digits.

if not(first.isdigit() and second.isdigit()):
  return 'Error: Numbers must only contain digits.'

Operands cannot be more than four digits.

if (length_1 > 4) or (length_2 > 4):
  return 'Error: Numbers cannot be more than four digits.'

Perform the arithmetic operations.

spacing = max(length_1, length_2)
if operator == '+':
  result = int(first) + int(second)
else:
  result = int(first) - int(second)           

Right justify the output lines.

line_1 = line_1 + first.rjust(spacing)  **#The problem is with this line.**
line_2 = line_2 + operator + second.rjust(spacing)
line_3 = line_3 + ''.rjust(spacing, '-')
line_4 = line_4 + str(result).rjust(spacing)

Create four spaces on each line to make room for the next problem.

if i < len(problems) - 1:
  line_1 += '    '  
  line_2 += '    '
  line_3 += '    '
  line_4 += '    ' 

Print out the results.

if statprint:
  arranged_problems = line_1 + line_2 + line_3 + line_4
else:
  arranged_problems = line_1 + line_2 + line_3

return arranged_problems

arithmetic_arranger([“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”])
arithmetic_arranger([“32 - 698”, “1 - 3801”, “45 + 43”, “123 + 49”], True)

Hi. In the description, I do not see if you are adding the newline \n between each of the lines when you’re constructing the arrange_problems.
Maybe your console output is just wide enough that it seems like you are printing to each line.

Example of using \n would be:

print("7" + "\n" + "+" + "\n" + "855")

or

print("7\n+\n855" )

which would both print out:

7
+
855

Here are some examples to help with the alignment problem which you mentioned.

>>> "+" + "855".rjust(3)
'+123'
>>> "7".rjust(3)
'  7'
>>> "7".rjust(3 +1)
'   7'

Also, remember that the instructions say there should always be a space after the operator.

print(len("+ "))
print("7".rjust(3+2))
print("+ " + "855".rjust(3))
print("".rjust(3+2, "-"))

Output:

2
    7
+ 855
-----

To test your code on this simple example of ["7 + 855"] you can go into main.py and write
print(arithmetic_arranger(["7 + 855"]))
on line 5. Then, the output of that print statement will be the first thing that will output in the output console.
You can also comment out the last line in main.py
main(module='test_module', exit=False)
to avoid the output from all the testing temporarily.

Hope this helps.