Tell us what’s happening:
My code complies with all the instructions and displays exact output yet I don’t understand why the Test run fails. I’m sharing a screenshot of the verbose output.
Kindly review my code and help!
Your code so far
def arithmetic_arranger(problems, show_answers=False):
operand_one_list = ''
operand_two_list = ''
base_line = ''
answer_list = ''
problems_length = len(problems)
if problems_length > 5 :
raise ValueError('Error: Too many problems.')
problems_clubbed = ''.join(problems)
for char in ' +-':
problems_clubbed = problems_clubbed.replace(char, '')
if problems_clubbed.isnumeric():
for problem in problems:
operand_one = problem[0:problem.index(' ')]
operator_sign = problem[problem.index(' ') + 1:problem.index(' ') + 2]
operand_two = problem[problem.index(' ')+3:]
sum_of_operands = ''
if len(operand_one) > 4 or len(operand_two) > 4:
raise ValueError('Error: Numbers cannot be more than four digits.')
else:
largest_operand = max(len(operand_one), len(operand_two))
for _ in range(largest_operand - len(operand_one)):
operand_one_list += ' '
operand_one_list += f' {operand_one} '
operand_two_list += f'{operator_sign} '
for _ in range(largest_operand - len(operand_two)):
operand_two_list += ' '
operand_two_list += f'{operand_two} '
for _ in range(largest_operand + 2):
base_line += '-'
base_line += ' '
if operator_sign == '+':
sum_of_operands = str(int(operand_one) + int(operand_two))
elif operator_sign == '-':
sum_of_operands = str(int(operand_one) - int(operand_two))
else:
raise ValueError("Error: Operator must be '+' or '-'.")
for _ in range((largest_operand + 2) - len(sum_of_operands)):
answer_list += ' '
answer_list += f'{sum_of_operands} '
else:
if (problems_clubbed.find('/') > -1) or (problems_clubbed.find('*') > -1):
raise ValueError("Error: Operator must be '+' or '-'.")
else:
raise TypeError('Error: Numbers must only contain digits.')
if show_answers is True:
return (f'{operand_one_list}\n{operand_two_list}\n{base_line}\n{answer_list}\n')
else:
return (f'{operand_one_list}\n{operand_two_list}\n{base_line}\n')
test1 = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
print(f'\nTest 1\n======\n{test1}')
test2 = arithmetic_arranger(["3 + 855", "988 + 40"], True)
print(f'\nTest 2\n======\n{test2}')
# test3 = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 3\n======\n{test3}')
# test4 = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 4\n======\n{test4}')
# test5 = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 5\n======\n{test5}')
# test6 = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
# print(f'\nTest 6\n======\n{test6}')
test7 = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
print(f'\nTest 7\n======\n{test7}')
test8 = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
print(f'\nTest 8\n======\n{test8}')
test9 = arithmetic_arranger(["1 + 2", "1 - 9380"])
print(f'\nTest 9\n======\n{test9}')
test10 = arithmetic_arranger(["3801 - 2", "123 + 49"])
print(f'\nTest 10\n=======\n{test10}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project