I don’t know why it is throwing errors when testing in replit, I’ve tried it on visual code and gives the same result as the expected result (I copied the expected_output string and printed it)
Your code so far
def arithmetic_arranger(problems, visiblity = False):
result = ""
problem_top = ""
problem_bottom = ""
problem_dash = ""
problem_result = ""
if len(problems)>5:
return "Error: Too many problems."
for problem in problems:
problem = str(problem)
problem = problem.split(" ")
try:
int(problem[0])
int(problem[2])
except:
return "Error: Numbers must only contain digits."
if problem[1] not in ["+","-"]:
return "Error: Operator must be '+' or '-'."
if len(problem[0])>4 or len(problem[2])>4:
return "Error: Numbers cannot be more than four digits."
count_1 = len(problem[0])
count_2 = len(problem[2])
len_dash = max(count_1,count_2) + 2
if problem[1] == "+":
result = str(int(problem[0]) + int(problem[2]))
else:
result = str(int(problem[0]) - int(problem[2]))
if problem != problems[-1]:
problem_top += str(problem[0]).rjust(len_dash) + " "
problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1) + " "
for r in range(len_dash):
problem_dash += "-"
problem_dash += " "
problem_result += str(result).rjust(len_dash) +" "
else:
problem_top += str(problem[0]).rjust(len_dash)
problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1)
for r in range(len_dash):
problem_dash += "-"
problem_result += str(result).rjust(len_dash)
if visiblity==True:
arranged_problems = (problem_top + "\n" + problem_bottom + "\n" + problem_dash + "\n" + problem_result )
else:
arranged_problems = (problem_top + "\n" + problem_bottom + "\n" + problem_dash )
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Challenge: Arithmetic Formatter
Link to the challenge: