Tell us what’s happening:
My code isn’t passing any of the non-error related tests, whenever I try running it in any shape or form it works, but it doesn’t pass any of the tests for some reason, even the console is showing it correctly, I have no idea why that is the case.
Your code so far
#Project - Build an Aritchmetic Formatter
def arithmetic_arranger(problems, show_answers=False):
# Error check
if len(problems) > 5:
return('Error: Too many problems.')
# Initializing variables that will be used later
operators = ["+", "-"]
digits = ['1','2','3','4','5','6','7','8','9','0']
problem_items = []
line01 = ''
line02 = ''
line03 = ''
line04 = ''
# Separates each part of the problem into the problem_items string
for problem in problems:
problem_items = problem.split(' ')
# Resets the max and min item length for each loop iteration
max_item_length = 0
min_item_length = 4
# Error checks
if len(problem_items[1]) > 4 or len(problem_items[2]) > 4:
return('Error: Numbers cannot be more than four digits.')
if problem_items[1] not in operators:
return("Error: Operator must be '+' or '-'.")
for digit in problem_items[0]:
if digit not in digits:
return('Error: Numbers must only contain digits.')
for digit in problem_items[2]:
if digit not in digits:
return('Error: Numbers must only contain digits.')
#Checks the max and minimum operand char length, excluding the operator
for item in problem_items:
if max_item_length < len(item) and item != '+' and item != '-':
max_item_length = len(item)
if min_item_length > len(item) and item != '+' and item != '-':
min_item_length = len(item)
# Determines the amount of spaces used in each line by checking the difference between the item lenghts
if max_item_length == min_item_length:
line01_spaces = ' '
line02_spaces = ' '
elif len(problem_items[0]) > len(problem_items[2]):
line01_spaces = ' '
line02_spaces = (max_item_length - min_item_length) * ' ' + ' '
else:
line01_spaces = (max_item_length - min_item_length) * ' ' + ' '
line02_spaces = ' '
# Checks how many dashes are needed for the 3rd line
line03_dashes = (max_item_length * '-') + '--'
# Builds each line based on the iteration of the for loop
line01 += line01_spaces + problem_items[0] + ' '
line02 += problem_items[1] + line02_spaces + problem_items[2] + ' '
line03 += line03_dashes + ' '
# Builds the 4th line if required to show answers
if show_answers == True:
answers = ''
if problem_items[1] == '-':
answers = int(problem_items[0]) - int(problem_items[2])
elif problem_items[1] == '+':
answers = int(problem_items[0]) + int(problem_items[2])
line04_spaces = (len(line03_dashes) - len(str(answers))) * ' '
line04 += line04_spaces + str(answers) + ' '
if show_answers == True:
print(f'{line01}\n{line02}\n{line03}\n{line04}')
elif show_answers == False:
print(f'{line01}\n{line02}\n{line03}')
return problems
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project