Maybe it’s not the prettiest code, but the output seems to match what is expected/required. But it fails ALL the checks. I can’t figure out why. Any ideas?
def arithmetic_arranger(problems, solutions=False):
equation_len = len(problems)
if equation_len > 5:
return print("Error: Too many problems.")
inpt_split = []
line1 = ""
line2 = ""
line3 = ""
line4 = ""
for calc in problems:
split_calc = calc.split()
inpt_split.append(split_calc)
for num1, term, num2 in inpt_split:
if term != "+" and term != "-":
return print("Error: Operator must be '+' or '-'.")
if len(num1) > 4 or len(num2) > 4:
return print("Error: Numbers cannot be more than four digits.")
try:
int(num1)
int(num2)
except ValueError:
return print("Error: Numbers must only contain digits.")
max_length = max(len(num1)+2, len(num2)+2)
line1 += " "*(max_length-len(num1)) + num1 + " "
line2 += term + " "*(max_length-len(num2)-1) + num2 + " "
line3 += "-"*max_length + " "
if term == "+":
sums = int(num1) + int(num2)
elif term == "-":
sums = int(num1) - int(num2)
line4 += " "*(max_length-len(str(sums))) + str(sums) + " "
if solutions == False:
line4 = ""
arranged_problems = print(f"{line1.rstrip()}\n{line2.rstrip()}\n{line3.rstrip()}\n{line4.rstrip()}")
return arranged_problems