Tell us what’s happening:
I think my code is doing what it’s supposed to, based on the instructions. But none of the tests are succeeding. The console shows that the function should return a string with the ‘\n’ character showing up. Do I need to make that character actually visible?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
final_string_1 = ' '
final_string_2 = ' '
final_string_3 = ' '
final_string_4 = ' '
operator_str = ''
max_number_length = []
le_grand_number_str = ''
le_grand_final_string = ''
alphabet_error = False
for i in problems:
top_number = i.split(' ', 1)[0]
le_grand_number_str += top_number
operator = i.split(' ', 2)[1]
operator_str += operator
bottom_number = i.split(' ', 2)[2]
le_grand_number_str += bottom_number
if not le_grand_number_str.isnumeric():
alphabet_error = True
break
a = len(top_number)
b = len(bottom_number)
max_number_length.append(max(a, b))
c = a - b
if c >= 0:
final_string_1 += ' ' * 2 + top_number
elif c < 0:
final_string_1 += ' ' * (2 + (c * -1)) + top_number
final_string_1 += ' ' * 4
if i == len(problems):
final_string_1 += ' '
final_string_2 += operator
if c >= 0:
final_string_2 += ' ' * (c + 1) + bottom_number
elif c < 0:
final_string_2 += ' ' + bottom_number
final_string_2 += ' ' * 4
if i == len(problems):
final_string_2 += ' '
final_string_3 += '-' * (max(a, b) + 2)
final_string_3 += ' ' * 4
if i == len(problems):
final_string_3 += ' '
evaluated_number = str(eval(i))
d = len(evaluated_number)
final_string_4 += ' ' * (max(a, b) + 2 - d)
final_string_4 += evaluated_number
final_string_4 += ' ' * 4
if i == len(problems):
final_string_4 += ' '
if '*' in operator_str or '/' in operator_str:
print('Error: Operator must be "+" or "-".')
elif len(problems) > 5:
print('Error: Too many problems.')
elif any(x > 4 for x in max_number_length):
print('Error: Numbers cannot be more than four digits.')
elif alphabet_error == True:
print('Error: Numbers must only contain digits.')
else:
le_grand_final_string = final_string_1 +'\n'+ final_string_2 +'\n'+ final_string_3
if show_answers == True:
le_grand_final_string += '\n'+ final_string_4
print(le_grand_final_string)
print('\n')
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)
arithmetic_arranger(["3801 - 2", "123 + 49"])
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["3 + 855", "988 + 40"], True)
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/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project