Tell us what’s happening:
any calculation i give it solve its just shows me the first error if the problems are greater than five even when i try to actually reduce the problems
please i know the code is a bit rough but i would appraciate a hint
Your code so far
def arithmetic_arranger(problems, show_answers=False):
for problem in problems:
#the format the answers will be in
parts = problem.split()
left = parts[0]
operator = parts[1]
right = parts[2]
#if the problems are more than 5
if len(problem) > 5:
return 'Error: Too many problems.'
#if the operator is not + or -
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'"
#if the number is not a digit
if not (left.isdigit() and right.isdigit()):
return 'Error: Numbers must only contain digits'
#if any of the opprand is greater than 4
if len(left) > 4 or len(right) > 4:
return 'Error: Numbers cannot be more than four digits.'
#for the lines
top_line = []
bottom_line = []
dash_line = []
answer = []
width = max(len(left), len(right)) + 2
top_line = left.rjust(width)
bottom_line = right.rjust(width - 1)
dash = '-' * width
if operator == '+':
result = str(int(left) + int(right))
else:
result = str(int(left) - int(right))
answer.append(resultr.just(width))
#arranged section
arranged_top = ' '.join(top_line)
arranged_bottom = ' '.join(bottom_line)
arranged_dash = ' '.join(dash_line)
if show_answer:
arranged_answer = ' '.join(answer)
return f"{arranged_top}\n{arranged_bottom}\n{arranged_dash}\n{arranged_answer}"
else:
return f"{arranged_top}\n{arranged_bottom}\n{arranged_dash}"
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 9"])}')
print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{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/140.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project