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
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.
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.
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.
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…
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.