my arithmetic arranger is failing on all but the verification tests and I haven’t been able to pin point why. Code below.
def arithmetic_arranger(problems, show_answer=False):
#creating variables for future use
first_line=''
second_line=''
dashes=''
sumline=''
#verification
if len(problems) > 5:
return 'Error: Too many problems.'
for item in problems:
digit_counter = item.split()
if digit_counter[0].isdigit() == False or digit_counter[2].isdigit() == False:
return "Error: Numbers must only contain digits."
if len(digit_counter[0]) > 4 or len(digit_counter[2]) > 4:
return "Error: Numbers cannot be more than four digits."
if digit_counter[1] != '+' and digit_counter[1] != '-':
return "Error: Operator must be '+' or '-'."
#now assiging variables and getting the length of each problem
num_one = digit_counter[0]
operator = digit_counter[1]
num_two = digit_counter[2]
total_length = max(len(num_one), len(num_two)) + 2
#getting answers and creating the answer string
if show_answer == True:
sumline += str(eval(item)).rjust(total_length) + ' '
#creating the strings
first_line += num_one.rjust(total_length)+' '
second_line += operator + num_two.rjust(total_length-1)+' '
dashes += ''.rjust(total_length,'-')+' '
#putting it all together
if show_answer == False:
arranged_problems =f'{first_line}\n{second_line}\n{dashes}'.rstrip()
else:
arranged_problems =f'{first_line}\n{second_line}\n{dashes}\n{sumline}'.rstrip()
print(arranged_problems)
return arranged_problems
I’ve thought it might be from the .rstrip() at the end of either string not working quite how I imagine but I’m not really sure.