# Arithmetic Formatter python print

i dont understand why the the top line with the first operand and the bottom line with the answers arent right aligned?

Please post your actual code instead of a screenshot. Also, please post a link to the project. Thanks

It looks like the first line is offset by the >>> prompt.

You could add a newline `"\n"` to your print, that should move it to the next line:

``````print('\n',arithmetic_arr....)
``````

hereâs the link i am unsure on how to you would want me to post the code, but ill copy and paste it under the link

heres the link

``````def arithmetic_arranger(problems, show_answers=True):
if len(problems) > 5:
return 'Error: Too many problems.'

arranged_problems1 = ""
arranged_problems2 = ""
arranged_problems3 = ""
arranged_answers = ""

for problem in problems:
operand1, operator, operand2 = problem.split()
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not operand1.isdigit() or not operand2.isdigit():
return 'Error: Numbers must only contain digits.'

if len(operand1) > 4 or len(operand2) > 4:
return 'Error: Numbers cannot be more than four digits.'
max_length = max(len(operand1), len(operand2)) + 2
arranged_problems1 += operand1.rjust(max_length) + '    '
arranged_problems2 += operator + operand2.rjust(max_length - 1) + '    '
arranged_problems3 += '-' * max_length + '    '
if show_answers:
if operator == '+':
answer = str(int(operand1) + int(operand2))
else:
answer = str(int(operand1) - int(operand2))
arranged_answers += answer.rjust(max_length) + '    '
arranged_problems = '\n'.join([arranged_problems1.strip(), arranged_problems2.strip(), arranged_problems3.strip(), arranged_answers.strip()])
return arranged_problems
print('\n', arithmetic_arranger(["3801 - 2", "123 + 49"]))
``````

it looks a bit better but still have the same issue

Thereâs no need for screenshots of code!

Here you are removing all whitespace from the beginning and end of the answers line.

suggestion, instead of having this:

use this:
`print('\n' + arithmetic_arranger(["3801 - 2", "123 + 49"]))`
so it doesnât add the space at the beginning of the line

1 Like

okay so now everything looks right but it tells me its wrong and i assume its because of the print at the end where i added â\nâ

``````
def arithmetic_arranger(problems, show_answers=True):
if len(problems) > 5:
return 'Error: Too many problems.'

arranged_problems1 = ""
arranged_problems2 = ""
arranged_problems3 = ""
arranged_answers = ""

for problem in problems:
operand1, operator, operand2 = problem.split()
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not operand1.isdigit() or not operand2.isdigit():
return 'Error: Numbers must only contain digits.'

if len(operand1) > 4 or len(operand2) > 4:
return 'Error: Numbers cannot be more than four digits.'
max_length = max(len(operand1), len(operand2)) + 2
arranged_problems1 += operand1.rjust(max_length) + '    '
arranged_problems2 += operator + operand2.rjust(max_length - 1) + '    '
arranged_problems3 += '-' * max_length + '    '
if show_answers:
if operator == '+':
answer = str(int(operand1) + int(operand2))
else:
answer = str(int(operand1) - int(operand2))
arranged_answers += answer.rjust(max_length) + '    '
arranged_problems = '\n'.join([arranged_problems1, arranged_problems2.strip(), arranged_problems3.strip(), arranged_answers])
return arranged_problems
print('\n' + arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
``````

If you remove the print you should get the same error.

The tests donât look at whatâs printed, they look at whatâs returned by the function.

The print is just for your testing.

Iâve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the âpreformatted textâ tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

an other way you can look at things:

`arithmetic_arranger(["3801 - 2", "123 + 49"])` should return ` 3801 123\n- 2 + 49\n------ -----` .

You can use the following code to compare your output with the requested string in a linear format. You can also use F12 and look at the browser console for an even different way to look at the difference between your code and the output.

``````print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
print(repr('  3801      123\n-    2    +  49\n------    -----'))
``````

this print the following, the top is your output and the bottom is the expected.

``````>>> '  3801      123    \n-    2    +  49\n------    -----\n  3799      172    '
>>> '  3801      123\n-    2    +  49\n------    -----'
``````

I see some issues with spaces, but also you have the results when not requested

I guess the problem i have is understanding how i would change my code to return what the test wants without starting fully over, because i feel like my code makes sense now and fulfills the initial task.

``````def arithmetic_arranger(problems, show_answers=True):
if len(problems) > 5:
return 'Error: Too many problems.'

arranged_problems1 = ""
arranged_problems2 = ""
arranged_problems3 = ""
arranged_answers = ""

for problem in problems:
operand1, operator, operand2 = problem.split()
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not operand1.isdigit() or not operand2.isdigit():
return 'Error: Numbers must only contain digits.'

if len(operand1) > 4 or len(operand2) > 4:
return 'Error: Numbers cannot be more than four digits.'
max_length = max(len(operand1), len(operand2)) + 2
arranged_problems1 += operand1.rjust(max_length) + '    '
arranged_problems2 += operator + operand2.rjust(max_length - 1) + '    '
arranged_problems3 += '-' * max_length + '    '
if show_answers:
if operator == '+':
answer = str(int(operand1) + int(operand2))
else:
answer = str(int(operand1) - int(operand2))
arranged_answers += answer.rjust(max_length) + '    '
arranged_problems = '\n'.join([arranged_problems1, arranged_problems2, arranged_problems3, arranged_answers])
return arranged_problems
print('\n' + arithmetic_arranger(["3801 - 2", "123 + 49"]))

``````

If your code is not returning the required strings, then itâs not fulfilling the task.

Make sure your whitespace perfectly matches the requirement. It looks like you are sticking extra spaces at the end of the lines for every problem, including the last on in the set.