Hi! I am having a formatting problem with my arithmetic_formatter function. I have passed 4 out of 6 tests so far. The problem is that when i print out the calculations i appear to have a single whitespace at the end of each line of strings (s1, s2, s3 and s4 - which represents the result). I don’t know how to get rid of them. Does anybody have any idea how? Been buggin be for some time and i would appreciate very much some help
Down here is my code. I tested it in vscode and it seemed to work fine, no whitespaces at the end of the outputs.
Here is the picture with the errors from replit:
def arithmetic_arranger(problems, *sol):
if len(problems) > 5:
return "Error: Too many problems."
list_p = []
for elem in problems:
list_p.append(elem)
s1 = ' '
s2 = ' '
s3 = ' '
s4 = ' '
for i in range(len(list_p)):
s = list_p[i].split()
if s[1] != "+" and s[1] != "-":
return "Error: Operator must be '+' or '-'."
for num in s[0]:
if not num.isnumeric():
return "Error: Numbers must only contain digits."
for num in s[2]:
if not num.isnumeric():
return "Error: Numbers must only contain digits."
if len(s[0]) > 4 or len(s[2]) > 4:
return "Error: Numbers cannot be more than four digits."
if s[1] == "+":
rez = int(s[0]) + int(s[2])
rez = str(rez)
else:
rez = str(int(s[0]) - int(s[2]))
if len(s[0]) <= len(s[2]):
s1 = s1 + f" {' ' * abs(len(s[2]) - len(s[0]))}{s[0]} "
s2 = s2 + f"{s[1]} {s[2]} "
s3 = s3 + f"{'-' * (2 + len(s[2]))} "
s4 = s4 + f"{' ' * (2 + len(s[2]) - len(rez))}{rez} "
else:
s1 = s1 + f" {s[0]} "
s2 = s2 + f"{s[1]} {' ' * abs(len(s[0]) - len(s[2]))}{s[2]} "
s3 = s3 + f"{'-' * (2 + len(s[0]))} "
s4 = s4 + f"{' ' * (2 + len(s[0]) - len(rez))}{rez} "
s1 = s1.rstrip()
s2 = s2.rstrip()
s3 = s3.rstrip()
s4 = s4.rstrip()
if sol:
arranged_problems = s1 + "\n"+ s2 + "\n"+ s3 + "\n"+ s4
else:
arranged_problems = s1 + "\n"+ s2 + "\n"+ s3
return arranged_problems