# Struggling with Arithmetic Formatter

Hi! Im struggling with this challenge for a long time. I watch tutorials, forum post, etc, etc for guiding me but even with all of that i cant figure out what is the problem with my code. In top of that, i cant read correctly the console for see what are the error that are throwing at me.

I will be so glad if you can help me because i am stuck and i need my certification : c

Thank you!

Your code so far

``````def arithmetic_arranger(problems, solver = False):

#Principal Variables

first_number = ""
operator = ""
second_number = ""
lines = ""
solution = ""

#First Check after the FOR loop

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

#Core FOR Loop

for problem in problems:
problem_array = problem.split()
first_number = problem_array
operator = problem_array
second_number = problem_array

#Checks for the items before the split

if first_number.isdigit() and second_number.isdigit():
if len(first_number) > 4 < len(second_number):
return "Error: Numbers cannot be more than four digits"
else:
return "Error: Numbers must only contain digits."

if operator == "+":
solution == int(first_number) + int(second_number)
elif operator == "-":
solution == int(first_number) - int(second_number)
else:
return "Error: Operator must be '+' or '-'."

#Distance comprobation

distance = max(len(first_number), len(second_number)) + 2

top_item = str(first_number.rjust(distance)) + "    "
bottom_item = str(operator.ljust(distance)) + " " +str(second_number.rjust(distance)) + " "
lines = ""
solution_item = str(solution.rjust(distance))
for line in range(distance):
lines += "-"

if solver == True:
operations = top_item + "/n" + bottom_item + "/n" + lines + "/n" + solution_item
else:
operations = top_item + "/n" + bottom_item + "/n" + lines

return operations

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
``````

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

Challenge: Arithmetic Formatter

Link to the challenge:

Please post a link to your replit so we can run the code and look at the error messages.

1 Like

If you can post a link to your code, then it would be easier for someone to help

1 Like

Here is the link to my replit code: https://replit.com/@MatiasCastro6/boilerplate-arithmetic-formatter-3#arithmetic_arranger.py

Thanks for your attention.

``````AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
assert '  123    /n+...   49 /n-----' == '  3801      ...----    -----'
``````
``````+   123    /n+        49 /n-----
-   3801      123
- -    2    +  49
- ------    -----
``````

Here we got the error message and the error formatted.
The green line is your code and if we look closely at it, at the expected red output AND the arguments, it seems like you are showing the calculations in reverse order.

1 Like

We use ‘\n’ for new line and not ‘/n’

2 Likes

When you are looping through the ‘top line’ , ‘bottom line’ and all of that, it would be best if you store each of them in a variable when looping or else, the ‘for’ loop is just gonna give you the last value of the iteration…
Here’s what I mean

``````a = ['a' ,'b' , 'c']
for alphabet in a:
``````

If you print your answer like this:

``````a = ['a' , 'b' , 'c']
for alphabet in a:
``````

It’s just going to give you ‘c’ as the final answer
Because for the first iteration, the alphabet is ‘a’ (without us doing something to it)
for the second iteration, the alphabet is ‘b’ (without us doing something to it)
and for the third iteration, the alphabet is ‘c’…which is what you’ll finally print out

So, i’ll suggest you do something within the code like this

``````a = ['a' , 'b' , 'c']
output = ''
for alphabet in a:
#Do something to the code
word = alphabet + 'y'
output = output + word
print(output)
``````

The final answer should look like this ‘aybycy’
Try to make this adjustment to your code first or else it will carry out the last problem in all the problems of your code

1 Like

Mmm, so, How i can do the padding in that way? Because for that i nedd like im doing, separate every number in a variable(first_number, operator, etc) if i want positioning them right.

If i make an array for store the operations i cant do the paggind like this:

``````if solver == True:
operations = top_item + "/n" + bottom_item + "/n" + lines + "/n" + solution_item
else:
operations = top_item + "/n" + bottom_item + "/n" + lines

return operations
``````

I cant figure out how to do that. Thanks any way for your response.

Try putting everything in one for loop and then create a general list where you can append all your answers
Here’s what I mean:

``````def arithmetic_arranger(problems, solver = False):

#Principal Variables
first_number = ""
operator = ""
second_number = ""
lines = ""
solution = ""

#First Check after the FOR loop

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

#Core FOR Loop

for problem in problems:
problem_array = problem.split()
first_number = problem_array
operator = problem_array
second_number = problem_array

#Checks for the items before the split

if first_number.isdigit() and second_number.isdigit():
if len(first_number) > 4 < len(second_number):
return "Error: Numbers cannot be more than four digits"
else:
return "Error: Numbers must only contain digits."

if operator == "+":
solution == int(first_number) + int(second_number)
elif operator == "-":
solution == int(first_number) - int(second_number)
else:
return "Error: Operator must be '+' or '-'."

#Distance comprobation

distance = max(len(first_number), len(second_number)) + 2

top_item = str(first_number.rjust(distance))
bottom_item = str(operator.ljust(distance)) + " " +str(second_number.rjust(distance))
lines = ""
solution_item = str(solution.rjust(distance))
for line in range(distance):
lines += "-"

if solver == True:
operations = top_item + "\n" + bottom_item + "\n" + lines + "\n" + solution_item
else:
operations = top_item + "\n" + bottom_item + "\n" + lines

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
``````

Though this code code won’t work perfectly , its just to let you know what you have to include and give you an idea also…
Note that i created a list with the variable name ‘final_answer’
I also added other preceeding codes under the ‘for loop’ and not outside of it
I also appended each ‘operations’ to ‘final_answer’ after each iterations
You should also note that what you’re finally going to return should be outside of the for loop or else you’ll only get the first problem solving instead of everything

1 Like

Thanks, It works, but the last problem is which the problems are printed in this way:

``````   32
+ 698
-----
730

3801
-    2
------
3799

45
+ 43
----
88

123
+  49
-----
172

32
+ 698
-----

3801
-    2
------

45
+ 43
----

123
+  49
-----
``````

I try use the join method with “\n”, " ", “”, etc but the elements doesnt shows in the screen in one line.

Hmmm, for that, we can do it this way instead of using final_answers list…

``````Try taking all the top_lines in one list (i.e top = [ ])
then bottom_lines in another list (example bottom = [ ])
and so on...
So, for each iteration try appending your top_lines to the top list
E.g top.append(top_lines)
So, you can then finally return ('    '.join(top) + '\n' +'    '.join(operator) + '    '.join(bottom) +'\n' +  '     '.join(solution)

``````
1 Like

If you still have questions, then just write about it and probably someone will do justice to it
Where i am here, it’s exactly 2:01 am and its way past my bed time
I have to go to bed now to prepare for a busy day tomorrow(lol ,well, today actually)…
I’ll see you later…bye

1 Like

Bye, thank you anyway. Im triying without a list and using a string:

``````def arithmetic_arranger(problems, solver = False):

#Principal Variables
first_number = ""
operator = ""
second_number = ""
lines = ""
solution = ""
operations = ""

#First Check after the FOR loop

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

#Core FOR Loop

for problem in problems:
first_number = problem.split()
operator = problem.split()
second_number = problem.split()

#Checks for the items before the split

if first_number.isdigit() and second_number.isdigit():
if len(first_number) > 4 < len(second_number):
return "Error: Numbers cannot be more than four digits"
else:
return "Error: Numbers must only contain digits."

if operator == "+":
solution = str(int(first_number) + int(second_number))
elif operator == "-":
solution = str(int(first_number) - int(second_number))
else:
return "Error: Operator must be '+' or '-'."

#Distance comprobation

distance = max(len(first_number), len(second_number)) + 2

top_item = str(first_number.rjust(distance)) + "    "
bottom_item = operator +str(second_number.rjust(distance - 1)) + "    "
lines = ""
solution_item = solution.rjust(distance) + "    "
for line in range(distance):
lines += "-"

if solver:
operations = operations + top_item + "\n" + bottom_item + "\n" + lines + "\n" + solution_item + "\n"

else:
operations = operations + top_item + "\n" + bottom_item + "\n" + lines + "\n"

return operations
``````

And its the same, the result is this:

``````   32
+ 698
-----
730
3801
-    2
------
3799
45
+ 43
----
88
123
+  49
-----
172
``````

i cant figure out how to print the operations in the same line. I think it would be more easy :s Goodnight.

Why not build each line inside the loop and put them into the same string outside of the loop?

1 Like

Mmm how?Im doing that right now, building top_item, bottom_item, etc for then, put it together in the “operations” variable.

You are adding to the operations string inside of your loop…

Use this idea and it should work
Note: This is just an idea, not a solution

Im going crazy, I tried with this idea, then others, try changing this over and over and the same problem…

Here’s a suggestion…
For every iteration in the ‘for loop’, try:
a) appending all the first numbers in a single list

``````Example: top_lines = ['345', '23', '54']
``````

b) appending all the operators and second numbers in a single list

``````Example: bottom_lines = ['+    45', '-      367', '+    8974']
``````

c) appending all the dashes in a single list

``````Example:   dashes = ['-----', '-------', '----']
``````

d) appending all the final solutions of the operations in a single list

``````Example: solution = ['answer1', 'answer2', 'answer3']
``````

e) finally, using the newline(\n) and join method to print out the result

``````Example: print('  '.join(top_line) + '\n' + '  '.join(middle_line) + '\n' + '  '.join(dashes) + '\n' + '  '.join(solution))
``````

I hope this helps

1 Like

Thanks, but i already tried that, i think its a thing in the code i cant see, a little detail like some identation, i dont know. Because i see other codes for this challenge where even without the use of lists the operations are printed out correctly in a single line, and are very simmilar to my first code posted here…

https://replit.com/@MatiasCastro6/boilerplate-arithmetic-formatter-3#arithmetic_arranger.py here its the try i make