I am failing for output formatting however, the comparison of outputs seem the same to me. Any idea what is wrong with this formatting … code is below: Thanks
import re
def spaces(count, val):
rtn = ""
for x in range(count):
rtn = rtn + val
return rtn
def arithmetic_arranger(problems, *true):
arranged_problems = ""
line1 = ""
line2 = ""
line3 = ""
line4 = ""
cnt = 0
for x in problems:
cnt += 1
if cnt > 5:
return "Error: Too many problems."
y = x.split()
try:
int(y[0])
int(y[2])
except:
return "Error: Numbers must only contain digits."
if y[1] == "+":
sum = int(y[0]) + int(y[2])
elif y[1] == "-":
sum = int(y[0]) - int(y[2])
else:
return "Error: Operator must be \'+\' or \'-\'."
sum = str(sum)
if len(y[0]) > len(y[2]):
space1 = len(y[0])
else:
space1 = len(y[2])
if len(y[0]) > 4 or len(y[2]) > 4:
return "Error: Numbers cannot be more than four digits."
spaceneeded = space1 - len(y[0]) + 2
line1 = line1 + spaces(spaceneeded," ")+y[0] + " "
spaceneeded = space1 - len(y[2]) + 1
line2 = line2 + "" + y[1] + spaces(spaceneeded," ") + y[2]+" "
spaceneeded = space1 + 2
line3 = line3 + "" + spaces(spaceneeded, "-")+" "
if true :
spaceneeded = space1 - len(str(sum)) + 2
line4 = line4 + " " + spaces(spaceneeded, " ") + sum + " "
expected = " 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----"
line1 = re.sub(" $","", line1)
line2 = re.sub(" $", "", line2)
line3 = re.sub(" $", "", line3)
line4 = re.sub(" $", "", line4)
expected = line1 + "\n" + line2 + "\n" + line3+ "\n"+line4
return expected
print (arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(" 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----")
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.
This assignment line was there for debugging/comparing the outputs. It was overwritten a few lines later. That section of code has changed to the below but still fails:
I looked for trim but didn’t find it, found strip() which also takes off leading white space. just found rstrip which looks like the same as trim… is there a direct trim function?
Yea I saw I was failing the True test, wanted to get past the general one first. Looks like I am passing now. Thanks a lot for your help.
~/boilerplate-arithmetic-formatter-1$ python test_module.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.001s
OK
~/boilerplate-arithmetic-formatter-1$