# Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
My code outputs the way it was asked to on my machine. But when running on the replit website, it doesn’t pass the tests… Why is that?
and i cant run the code with the run button

Your code so far

``````import re

numb1 = []
numb2 = []
opp = []
res = []

def verification(list):
if len(list) > 5:
print("Error: Too many problems.")

for i in list:

if re.search("[A-Za-z]", i):
print("Error: Numbers must only contain digits.")

if re.search(r".*\s[/*]\s.*\$", i):
print("Error: Operator must be '+' or '-'.")

numb = i.split(" ")
if len(numb[0]) > 4 or len(numb[2]) > 4:
print("Error: Numbers cannot be more than four digits.")

else:
numb1.append(numb[0])
opp.append(numb[1])
numb2.append(numb[2])

if numb[1] == "+":
res.append(str(int(numb[0]) + int(numb[2])))
elif numb[1] == "-":
res.append(str(int(numb[0]) - int(numb[2])))

def show(result):
linha1 = ""
linha2 = ""
linha3 = ""
linha4 = ""

for i in range(len(res)):
if len(numb1[i]) > len(numb2[i]):
n2 = 1 + len(numb1[i]) - len(numb2[i])
linha1 += "  " + numb1[i]
linha2 += opp[i] + " " * n2 + numb2[i]
sep = 2 + len(numb1[i])
linha3 += "-" * sep

elif len(numb1[i]) <= len(numb2[i]):
n1 = 2 + len(numb2[i]) - len(numb1[i])
linha1 += " " * n1 + numb1[i]
linha2 += opp[i] + " " + numb2[i]
sep = 2 + len(numb2[i])
linha3 += "-" * sep

m = max(len(numb1[i]), len(numb2[i]))
if len(res[i]) > m:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

mi = min(len(numb1[i]), len(numb2[i]))
if len(res[i]) == m:
sp = 2
linha4 += " " * sp + res[i]

if len(res[i]) < mi:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

if i != len(res) - 1:
space = " " * 4
linha1 += space
linha2 += space
linha3 += space
linha4 += space

print(linha1 + "\n" + linha2 + "\n" + linha3)

if result is True:
print(linha4)

def arithmetic_arranger(list, result=False):
verification(list)
show(result)
``````

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

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 (').

Printing is not the same thing as returning the required string.

I suspect you also have extra spaces at the end of every line.

Also, your use of global variables means your function can only be called once (which defeats the purpose of a function), but the test suite calls your function several times.

thanks for the tip! I have already corrected the print to return, and now I pass 4 tests…
I don’t know what’s wrong…

``````import re
numb1 = []
numb2 = []
opp = []
res = []
def arithmetic_arranger(list, result=False):
if len(list) > 5:
return "Error: Too many problems."

for i in list:

if re.search("[A-Za-z]", i):
return "Error: Numbers must only contain digits."

if re.search(r".*\s[/*]\s.*\$", i):
return "Error: Operator must be '+' or '-'."

numb = i.split(" ")
if len(numb[0]) > 4 or len(numb[2]) > 4:
return "Error: Numbers cannot be more than four digits."

else:
numb1.append(numb[0])
opp.append(numb[1])
numb2.append(numb[2])

if numb[1] == "+":
res.append(str(int(numb[0]) + int(numb[2])))
elif numb[1] == "-":
res.append(str(int(numb[0]) - int(numb[2])))

linha1 = ""
linha2 = ""
linha3 = ""
linha4 = ""

for i in range(len(res)):
if len(numb1[i]) > len(numb2[i]):
n2 = 1 + len(numb1[i]) - len(numb2[i])
linha1 += "  " + numb1[i]
linha2 += opp[i] + " " * n2 + numb2[i]
sep = 2 + len(numb1[i])
linha3 += "_" * sep

elif len(numb1[i]) <= len(numb2[i]):
n1 = 2 + len(numb2[i]) - len(numb1[i])
linha1 += " " * n1 + numb1[i]
linha2 += opp[i] + " " + numb2[i]
sep = 2 + len(numb2[i])
linha3 += "_" * sep

m = max(len(numb1[i]), len(numb2[i]))
if len(res[i]) > m:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

mi = min(len(numb1[i]), len(numb2[i]))
if len(res[i]) == m:
sp = 2
linha4 += " " * sp + res[i]

if len(res[i]) < mi:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

if i != len(res) - 1:
space = " " * 4
linha1 += space
linha2 += space
linha3 += space
linha4 += space

if result == True:
return linha1 + "\n" + linha2 + "\n" + linha3 + "\n" + linha4
else:
return linha1 + "\n" + linha2 + "\n" + linha3
``````

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 (').

Have you done anything about these problems?

sorry for the duplicate post.
about the spaces, I have 4 spaces between each problem, except for the last one that doesn’t have.
About the variables I don’t know what to do…

To fix global variables, you put your variables inside of a smaller scope.

For the spaces, you should have four spaces between each problem but should not have four extra spaces at the end of every line.

like this? continues to pass in just 4 tests

``````def arithmetic_arranger(problems, result=False):
import re
numb1 = []
numb2 = []
opp = []
res = []
linha1 = ""
linha2 = ""
linha3 = ""
linha4 = ""

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

for i in problems:

if re.search("[A-Za-z]", i):
return "Error: Numbers must only contain digits."

if re.search(r".*\s[/*]\s.*\$", i):
return "Error: Operator must be '+' or '-'."

numb = i.split(" ")
if len(numb[0]) > 4 or len(numb[2]) > 4:
return "Error: Numbers cannot be more than four digits."

else:
numb1.append(numb[0])
opp.append(numb[1])
numb2.append(numb[2])

if numb[1] == "+":
res.append(str(int(numb[0]) + int(numb[2])))
elif numb[1] == "-":
res.append(str(int(numb[0]) - int(numb[2])))

for i in range(len(res)):
if len(numb1[i]) > len(numb2[i]):
n2 = 1 + len(numb1[i]) - len(numb2[i])
linha1 += "  " + numb1[i]
linha2 += opp[i] + " " * n2 + numb2[i]
sep = 2 + len(numb1[i])
linha3 += "_" * sep

elif len(numb1[i]) <= len(numb2[i]):
n1 = 2 + len(numb2[i]) - len(numb1[i])
linha1 += " " * n1 + numb1[i]
linha2 += opp[i] + " " + numb2[i]
sep = 2 + len(numb2[i])
linha3 += "_" * sep

m = max(len(numb1[i]), len(numb2[i]))
if len(res[i]) > m:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

mi = min(len(numb1[i]), len(numb2[i]))
if len(res[i]) == m:
sp = 2
linha4 += " " * sp + res[i]

if len(res[i]) < mi:
sp = 2 + m - len(res[i])
linha4 += " " * sp + res[i]

if i != len(res) - 1:
space = " " * 4
linha1 += space
linha2 += space
linha3 += space
linha4 += space

if result == True:
return linha1 + "\n" + linha2 + "\n" + linha3 + "\n" + linha4
else:
return linha1 + "\n" + linha2 + "\n" + linha3

``````

This looks much closer.

This would be much easier if you provide the test output. Unfortunately, I can’t catch all errors just by looking at your raw source code and guessing.

I hope you can see it here

https://replit.com/@RicardoBarbos14/boilerplate-arithmetic-formatter#arithmetic_arranger.py

``````-   3801      123
?           -----
+   3801
- -    2    +  49
?           -----
+ -    2
- ------    -----
+ ______
``````
``````-   1         1
- + 2    - 9380
- ---    ------
+   1
+ + 2
+ ___
``````
``````-     3      3801      45      123
- + 855    -    2    + 43    +  49
- -----    ------    ----    -----
+     3
+ + 855
+ _____
``````

I am seeing two issues here

1. It looks like you are returning prematurely

2. It looks like you are using the wrong sorts of dashes

Thank you so much for your time, keep up the good work!!!

1 Like

Good work getting it passing!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.