Tell us what’s happening:
I’ve tried everything and the output is the same as what the tests are asking for but they do not work except for the tests on errors.
Your code so far
def format (lines1, lines2, lines3, totals = ""):
lines = ""
line1 = ""
line2 = ""
line3 = ""
line4 = ""
for line in lines1:
line1 += (line)
for line in lines2:
line2 += (line)
for line in lines3:
line3 += (line)
for total in totals:
line4 += total
lines += line1 + "\n"
lines+= line2 + "\n"
lines+= line3 + "\n"
lines+= line4 + "\n"
return lines
def sort_equations(problems, show_answers = False):
error = ""
lines1 = []
lines2 = []
lines3 = []
lines4 = []
#seperate and sort needed values
digit0 =[]
digit1 = []
for problem in problems:
error = ""
digit = []
prob = problem.split()
op = ""
for p in prob:
if p.isnumeric():
digit.append(p)
elif p == "+":
op = "+"
elif p == "-":
op = "-"
if op == "":
error = "Error: Operator must be '+' or '-'."
if len(digit) != 2:
error = "Error: Numbers must only contain digits."
for d in digit:
if len(d) > 4:
error = "Error: Numbers cannot be more than four digits."
if error != "":
return error
if op == "+":
total = int(digit[0]) + int(digit[1])
elif op == "-":
total = int(digit[0]) - int(digit[1])
total = str(total)
digit0.append(digit[0])
digit1.append(digit[1])
#find longest length for num of spaces and dashes needed
if len(digit[0]) > len(digit[1]):
longest = len(digit[0])
diff = len(digit[0]) - len(digit[1])
elif len(digit[0]) < len(digit[1]):
longest = len(digit[1])
diff = len(digit[1]) - len(digit[0])
else:
longest = len(digit[0])
diff = 0
#find spaces needed
spaces = " "
dashes = ""
for i in range(longest + 2):
dashes += "-"
for i in range(diff):
spaces += " "
sp = len(dashes) - len(total)
ans_sp = ""
for i in range(sp):
ans_sp += " "
#format everything together
if longest == len(digit[0]):
line1 = " " + digit[0] + " "
line2 = op+ spaces + digit[1] + " "
line3 = dashes + " "
line4 = ans_sp + total + " "
elif longest == len(digit[1]):
line1 = " " +spaces + digit[0] + " "
line2 = op + " " + digit[1]+" "
line3 = dashes + " "
line4 = ans_sp + total+ " "
lines1.append(line1)
lines2.append(line2)
lines3.append(line3)
lines4.append(line4)
if show_answers == True:
lines = format(lines1, lines2, lines3,lines4)
else:
lines = format(lines1, lines2, lines3)
return lines
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return "Error: Too many problems."
else:
if show_answers == True:
lines = sort_equations(problems, True)
else:
lines = sort_equations(problems)
return lines
def main():
print("------Arithmetic Formatter------")
print("\n")
#Test1
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
#Test2
print(f'\n{arithmetic_arranger(["1 + 2", "1 - 9380"])}')
#Test3
print(f'\n{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
#Test4
print(f'\n{arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])}')
#Test5
print(f'\n{arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])}')
#Test6
print(f'\n{arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])}')
#Test7
print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')
#Test8
print(f'\n{arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])}')
#Test9
print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
#Test10
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')
main()
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
