Tell us what’s happening:
I don’t really understand the issue with my code. It prints nicely formatted equations.
Your code so far
def arithmetic_arranger(equations, *boolean):
assert len(equations) <= 5, "Error: Too many problems."
upper_half_list = []
lower_half_list = []
line_list = []
result_list =[]
for equation in equations:
content = equation.split()
operator = content[1]
assert len(content[0]) <= 4, "Error: Numbers cannot be more than four digits."
assert len(content[2]) <= 4, "Error: Numbers cannot be more than four digits."
try:
number_1 = int(content[0])
number_2 = int(content[2])
except:
print("Error: Numbers must only contain digits.")
break
assert operator == "+" or operator == "-", "Error: Operator must be \"+\" or \"-\"."
if True in boolean:
if operator == "+":
result = str(number_1 + number_2)
elif operator == "-":
if number_1 < number_2:
result = '- ' + str(- (number_1 - number_2))
else:
result = str(number_1 - number_2)
else:
result = ""
if len(str(number_1)) < len(str(number_2)) + 2:
space = len(str(number_2)) + 2 - len(str(number_1))
line = (len(str(number_2)) + 2) * '-'
upper_half = space * ' ' + str(number_1)
lower_half = operator + ' ' + str(number_2)
else:
space = len(str(number_1)) - len(str(number_2))
line = (len(str(number_1)) + 1) * '-'
upper_half = ' ' + str(number_1)
lower_half = operator + space * ' ' + str(number_2)
result = (len(line) - len(result)) * ' ' + result
problem = upper_half + '\n' + lower_half + '\n' + line + '\n' + result
upper_half_list.append(upper_half)
lower_half_list.append(lower_half)
line_list.append(line)
result_list.append(result)
if len(equations) == 0:
print("No equations!")
elif len(equations) == 1:
print("{0}".format(upper_half_list[0]))
print("{0}".format(lower_half_list[0]))
print("{0}".format(line_list[0]))
print("{0}".format(result_list[0]))
elif len(equations) == 2:
print("{0} {1}".format(upper_half_list[0], upper_half_list[1]))
print("{0} {1}".format(lower_half_list[0], lower_half_list[1]))
print("{0} {1}".format(line_list[0], line_list[1]))
print("{0} {1}".format(result_list[0], result_list[1]))
elif len(equations) == 3:
print("{0} {1} {2}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2]))
print("{0} {1} {2}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2]))
print("{0} {1} {2}".format(line_list[0], line_list[1], line_list[2]))
print("{0} {1} {2}".format(result_list[0], result_list[1], result_list[2]))
elif len(equations) == 4:
print("{0} {1} {2} {3}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2], upper_half_list[3]))
print("{0} {1} {2} {3}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2], lower_half_list[3]))
print("{0} {1} {2} {3}".format(line_list[0], line_list[1], line_list[2], line_list[3]))
print("{0} {1} {2} {3}".format(result_list[0], result_list[1], result_list[2], result_list[3]))
elif len(equations) == 5:
print("{0} {1} {2} {3} {4}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2], upper_half_list[3], upper_half_list[4]))
print("{0} {1} {2} {3} {4}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2], lower_half_list[3], lower_half_list[4]))
print("{0} {1} {2} {3} {4}".format(line_list[0], line_list[1], line_list[2], line_list[3], line_list[4]))
print("{0} {1} {2} {3} {4}".format(result_list[0], result_list[1], result_list[2], result_list[3], result_list[4]))
EDIT: readibility
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0
Challenge: Arithmetic Formatter
Link to the challenge: