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)