Tell us what’s happening:
I can’t get pass this test. Please point out the problem for me.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
# Number/Operator contain list
operator = []
num_list = []
final_num_list = []
# Printing variable
first_line = []
second_line = []
line_list = []
sum_line = []
# Too many problems
if len(problems) > 5 :
return('Error: Too many problems.')
# Split part of problem
for problem in problems:
num = problem.split()
operator.append(num[1])
num_list.append(num[0])
num_list.append(num[2])
cross_line = ''
dash = ' ' * 4
# Digits check
for number in num_list:
if not number.isdigit():
return('Error: Numbers must only contain digits.')
# Digit width check
elif len(number) > 4:
return('Error: Numbers cannot be more than four digits.')
# Calculation
# Plus case
if num[1] == '+':
final_sum = int(num[0]) + int(num[2])
# Minus case
elif num[1] == '-':
final_sum = int(num[0]) - int(num[2])
legth = max(len(num[0]), len(num[2]))
final_leght = legth + 2
# Line maker
for i in range(final_leght):
cross_line += '-'
# Make number even digit
# First line
if len(num[0]) < final_leght:
for i in range(final_leght - len(num[0])):
num[0] = ' ' + num[0]
# Second line
if len(num[2]) < final_leght :
for i in range(final_leght - len(num[2]) - 1):
num[2] = ' ' + num[2]
num[2] = num[1] + num[2]
# For sum
if len(str(final_sum)) < final_leght :
for i in range(final_leght - len(str(final_sum))):
final_sum = ' ' + str(final_sum)
# Containing data
first_line.append(num[0] + dash)
second_line.append(num[2] + dash)
line_list.append(cross_line + dash)
sum_line.append(final_sum + dash)
# Check operator
for op in operator:
if op in ['*', '/']:
return("Error: Operator must be '+' or '-'.")
if show_answers:
answer = f"{''.join(first_line)}\n{''.join(second_line)}\n{''.join(line_list)}\n{''.join(sum_line)}"
else:
answer = f"{''.join(first_line)}\n{''.join(second_line)}\n{''.join(line_list)}"
return answer
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project