So i have been stuck on this problem for a long time I’m neary done but my code seems to mess up at this problem. [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]
def arithmetic_arranger(problems, answer = False):
#sets up list to be used later
over_list = list()
middel_list = list()
lower_list = list()
the_main_list = problems
#checks if there is more then 4 problems
if len(the_main_list) > 5:
return('Error: Too many problems.')
#checks if the input is numbers and not charachters
for one_cal in the_main_list:
one_problem = one_cal.split()
try:
one = int(one_problem[0])
two = int(one_problem[2])
except:
return("Error: Numbers must only contain digits.")
#checks if the problem is - or +
thing = one_problem[1] == '-' or one_problem[1] == '+'
if not thing:
return("Error: Operator must be '+' or '-'.")
lengt = len(one_problem[0]) < 5 and len(one_problem[2]) < 5
#checks if the problem is longer then 4 digets
if not lengt:
return('Error: Numbers cannot be more than four digits.')
#Calcuats the aswers if requested
aswer_list = list()
if answer:
for x in the_main_list:
y = x.split()
number_one = int(y[0])
number_two = int(y[2])
if y[1] == '+':
asnwer3 = number_one + number_two
elif y[1] == '-':
asnwer3 = number_one - number_two
if asnwer3 < 0:
aswer_with_backspace = ' ' + str(asnwer3)
aswer_list.append(aswer_with_backspace)
else:
aswer_with_backspace = ' ' + str(asnwer3)
aswer_list.append(aswer_with_backspace)
#Splits the problems and add the nessery spaces
for x in problems:
y = x.split()
if len(y[0]) < 1 and len([2]) < 1:
lengt_of_under = 4
elif len(y[0]) >= len(y[2]):
lengt_of_under = len(y[0]) + 2
elif len(y[0]) <= len(y[2]):
lengt_of_under = len(y[2]) + 2
backspace_over = lengt_of_under - len(y[0])
adfada = ' ' * backspace_over
lower_things = '-' * lengt_of_under
lower_space_numbers = lengt_of_under - len(y[2]) - 1
lower_with_things = ' '*lower_space_numbers
middel_list.append(y[1] + lower_with_things + y[2])
over_list.append(adfada+y[0])
lower_list.append(lower_things)
#Arranges the list to be the way that is requsted
Arragen = [over_list,middel_list, lower_list, aswer_list]
arranged_problems = ("\n".join(map(" ".join, Arragen)))
return arranged_problems
Challenge: Arithmetic Formatter
Link to the challenge: