# Help with the "Arithmetic-Formatter

Hi guys,
I’m having some troubles completing this first exercise. The thing is I wrote the code first in my text editor and it runs pretty well (is messy I know, and I still have some problems with the right-align). Yet Replit keeps telling me that my code doesn’t work in terms of properly sending the error’s messages. You know what might be wrong here? Once again I’m sorry on how messy the code is, I sure need to change some other things because how much code I wrote.

``````    numberProblems = len(problems)
if numberProblems < 5:
for i in range(len(problems)):
newOperation = problems[i].split()
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
if newOperation[1] == "+" or newOperation[1] == "-":
isValid = isValid + 1
else:
print("Error: Operator must be '+' or '-'.")
break
else:
print("Error: Numbers cannot be more than four digits.")
break
except:
print("Error: Numbers must contain digits.")
quit()
else:
print("Error: Too many problems.")
if isValid == numberProblems:
for i in range(len(problems)):
newOperation = problems[i].split()
firstOperand = f"{newOperation[0]:>5}"
print(firstOperand)
secondOperand = f"{newOperation[2]:>3}"
print(newOperation[1], secondOperand)
line = ""
if len(newOperation[0]) > len(newOperation[2]):
for y in range(len(newOperation[0]) + 2):
line = line + "-"
else:
for y in range(len(newOperation[2]) + 2):
line = line + "-"
print(line)
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
finalResult = f"{result:>5}"
print(finalResult)
else:
result = int(newOperation[0]) - int(newOperation[2])
finalResult = f"{result:>5}"
print(finalResult)
whiteSpace = "\n"
print(3*whiteSpace)

arithmetic_arranger(["32 + 698", "3815 - 2", "45 + 43", "123 + 49"])

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 OPR/78.0.4093.147</code>

**Challenge:**  Arithmetic Formatter

https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter``````

You need to return the error message instead of printing it.

Thank you so much Jeremy! I can’t believe I didn’t realize of such a silly mistake myself. That been said, although now there are less errors, Replit tells me of three errors which I can’t figure out how to fix them. “Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801- 2”, “45 + 43”, “123 + 49”]”; "Expected… to return “Error: Numbers must only cointain digist.”; “Expected solution to be correctly displayed in output when calling “arithmetic_arranger()” with aritmetic problems and a second argument of ‘True’”.

1 Like

I’d need to see your updated code to say much.

Though, I suspect that you you do not have an optional argument that checks if the solutions should be included in the output.

Make sure you change your print statements into returns for both the error messages and the actual result.

1 Like

First of all, sorry about the other post. Turns out I didn’t copy correctly my code from my text editor.
Second, I’m having some struggles returning the actual result like Ibrahim said. Putting it just out of the “for” just gives me a list. Otherwise I don’t know what to do since just changing the “print” as I used before (see my first post) doesn’t work anymore.
Here’s my updated code:

``````def arithmetic_arranger(problems, isValid = False):
numberProblems = len(problems)
if numberProblems < 5:
for i in range(len(problems)):
newOperation = problems[i].split()
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
if newOperation[1] == "+" or newOperation[1] == "-":
isValid = True
else:
return "Error: Operator must be '+' or '-'."
else:
return"Error: Numbers cannot be more than four digits."
except:
return "Error: Numbers must contain digits."
else:
return "Error: Too many problems."
if isValid:
for i in range(len(problems)):
newOperation = problems[i].split()
firstOperand = f"{newOperation[0]:>5}"
secondOperand = f"{newOperation[2]:>3}"
line = ""
if len(newOperation[0]) > len(newOperation[2]):
for y in range(len(newOperation[0]) + 2):
line = line + "-"
else:
for y in range(len(newOperation[2]) + 2):
line = line + "-"
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
finalResult = f"{result:>5}"
else:
result = int(newOperation[0]) - int(newOperation[2])
finalResult = f"{result:>5}"
whiteSpace = "\n"
return firstOperand, newOperation[1], secondOperand, line, result, whiteSpace
``````

I’ve edited your post 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 (’).

I don’t think this if statement does what you want. If the parameter value is `False`, then you should return the problems without the solutions. If the parameter value is `True`, then you should return the problems with the solutions.

I would use a more descriptive name for that parameter, like `showSolutions`.

My bad, I’m sorry. I’ll use the backticks from here on out every time I post code. Thanks for the tip!

So you are saying that, after sending the error message to the user, I must also give him the problem (with the first operand up, the operator and second operand in the middle and the lines below) but without the results?

No. I’m saying that the parameter you named `isValid` here

should actually be a parameter that determines if the solutions are shown or not.

To return your final result, you should concatenate all your separate strings into one string before returning the new string. As you said, returning multiple values at once will result in a list of all the values.

Ohhhhh, now I see. Give me some time so I can re-structure my code.

For clarification purposes, I believe Jeremy is referencing this quote from the project instructions:

“The function should optionally take a second argument. When the second argument is set to `True`, the answers should be displayed.”

In your code, the parameter determines whether or not to return any of your results, rather than determining whether or not to return the answers.

1 Like

I was about to try that but now your comment gives me even more confidence in that idea. Thank you

No problem! Always happy to help!

I think I make so progress but I got stuck in something I don’t understand:

``````def arithmetic_arranger(problems, showResults = False):
numberProblems = len(problems)
if numberProblems < 5:
for i in range(len(problems)):
newOperation = problems[i].split()
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
firstOperand = f"{newOperation[0]:>5}"
secondOperand = f"{newOperation[2]:>3}"
line = ""
if len(newOperation[0]) > len(newOperation[2]):
for y in range(len(newOperation[0]) + 2):
line = line + "-"
else:
for y in range(len(newOperation[2]) + 2):
line = line + "-"
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
elif newOperation[1] == "-":
result = int(newOperation[0]) - int(newOperation[2])
else:
return "Error: Operator must be '+' or '-'."
finalResult = f"{str(result):>5}"
>                     answer = answer + firstOperand + "\n" + newOperation[1] + " " + secondOperand + "\n" + line + "\n" + finalResult
showResults = True
else:
return "Error: Numbers cannot be more than four digits."
except:
return "Error: Numbers must contain digits."
else:
return "Error: Too many problems."
if showResults:
``````

Is with the variable “answer” part, I don’t get it. If it’s an iteration why does it only saves the last part. It’s literally breaking my mind :(.

I figured out the answer: I had to put the empty “answer” variable just outside of the “for”. Although I’m still not sure why, so my question still stands.

When you define a variable within a for loop, the variable is defined again every iteration. For example, if I have something like the following:

``````for _ in range(5):
num = 0
num = num + 1

return num
``````

You might expect this to return 5, but it doesn’t. This is because we make num equal to 0 every iteration before we add one. So in every iteration, num goes back to 0, then to 1, and on the next iteration, it will go back to 0, and so on, until the final iteration where it will stop, therefore looking as though it only ran the final iteration.

Ohhh, now I get it! Thank you.