Tell us what’s happening:
My code fails the first 4 and last 2 tests. It is weird because when I check them, they work fine. I have tried looking in the developer console but I have hit a wall. At this point I have spent a little less than half the time I spent on coding to debug this and I have absolutely given up. If anyone can nudge me in the right direction, I would really appreciate it. Apologies for having such a clunky and often redundant code. I am not sure where the formatting error is especially since I have personally check amount of spaces and other items. Again, I am not sure what the error is just that it makes my code fail the first 4 and last 2 tests.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) <= 5:
list_of_values = []
first_set_of_number = []
second_set_of_number = []
operator_holding_list = []
length_of_first_operands = []
length_of_second_operands = []
number_of_dashes = []
spaces_needed_for_top_operand = []
spaces_needed_for_bottom_operand = []
finalized_first_line = []
finalized_second_line = []
answers = []
for item in problems:
item = (item.replace(" ", "")).strip()
temp_list_of_values = []
# to validate input in terms of correct chars that is each character
# must be a digit or + or -.
# if not output/print a designated error message
for char in item:
if not char.isdigit():
if char == "+" or char == "-":
pass
elif char.isalpha():
return "Error: Numbers must only contain digits."
else:
return "Error: Operator must be '+' or '-'."
# we add a desginated '!' at the end so we can terminate at desired
# time when iterating in the below for loop
item = item + "!"
# using loop and conditional iterate through each char and seperate
# into a temp_list, once the loop reaches a '+' or '-' it should
# join temp_list and add it to the list_of_values than add the char
# it was iterating to the list_of_values as well. Lastly, null the
# temp_list and repeat
# modified the designated last char in each item to "!" to ensure
# a '+' or '-' is not added to the list if not inputed by user
for char in item:
for value in list_of_values:
if len(value) > 4:
return "Error: Numbers cannot be more than four digits."
if char.isdigit():
temp_list_of_values.append(char)
elif char == "+" or char == "-":
list_of_values.append("".join(temp_list_of_values))
list_of_values.append(char)
temp_list_of_values = []
else:
list_of_values.append("".join(temp_list_of_values))
temp_list_of_values = []
# debug
# print("Item in the input set seperated:", item)
# print("\nThis is the list of values after items are seperated:", list_of_values)
## separate everything by category for ease of use later, i.e. first operand, second operand, and operator but also find min
# check and get the first operand for each arthematic sequence in a seperate list
for i in range(0, len(list_of_values), 3):
first_set_of_number.append(list_of_values[i])
length_of_first_operands.append(len(list_of_values[i]))
# debug
# print("\nThis is the list of operands that will go in the first print line:", first_set_of_number)
# check and get the second operand for each arthimatic sequence in another separate list
for i in range(2, len(list_of_values), 3):
second_set_of_number.append(list_of_values[i])
length_of_second_operands.append(len(list_of_values[i]))
# debug
# print("\nThis is the list of the operands that will go on the second print line:", second_set_of_number)
# get the operators in a seperate list for later use
for i in range(1, len(list_of_values), 3):
operator_holding_list.append(list_of_values[i])
# debug
# print("\nThese are the operators being used for each arthematic:", operator_holding_list)
# print("\nLength of each value in the first line operands:", length_of_first_operands)
# print("Length of each value of the second line operands:", length_of_second_operands)
# because more works need to be done to get the actual number for how many dashes need to be placed so we will label this as such for it is the max when comparing the length between two operands from same problem
max_length = []
for i in range(len(length_of_second_operands)):
largest_length = max(length_of_second_operands[i], length_of_first_operands[i])
max_length.append(largest_length)
# print("Compared set of values between first line and second line operand to find the one that is max:", max_length)
for inst in range(len(max_length)):
dashes_amount = max_length[inst] + 2
number_of_dashes.append(dashes_amount)
spaces_for_top = dashes_amount - length_of_first_operands[inst]
spaces_needed_for_top_operand.append(spaces_for_top)
spaces_for_bottom = dashes_amount - length_of_second_operands[inst] - 1
spaces_needed_for_bottom_operand.append(spaces_for_bottom)
# print("\nNumber of dashes to be placed for each arthematic:", number_of_dashes)
# print("Number of spaces to leave for first operand:", spaces_needed_for_top_operand)
# print("Number of spaces to leave between operator and operand on second line:", spaces_needed_for_bottom_operand)
list_to_hold_amount_of_spaces_for_top_operands = []
for each_amount_of_spaces in spaces_needed_for_top_operand:
variable = ""
for i in range(each_amount_of_spaces):
variable += " "
list_to_hold_amount_of_spaces_for_top_operands.append(variable)
variable = ""
for i in range(len(first_set_of_number)):
if i != len(first_set_of_number) - 1:
temp_holder = list_to_hold_amount_of_spaces_for_top_operands[i] + first_set_of_number[i] + " "
finalized_first_line.append(temp_holder)
else:
temp_holder = list_to_hold_amount_of_spaces_for_top_operands[i] + first_set_of_number[i]
finalized_first_line.append(temp_holder)
# print("\nFirst Line to be printed:", finalized_first_line)
list_to_hold_amount_of_spaces_for_bottom_operands = []
for each_amount_of_spaces in spaces_needed_for_bottom_operand:
variable = ""
for i in range(each_amount_of_spaces):
variable += " "
list_to_hold_amount_of_spaces_for_bottom_operands.append(variable)
variable = ""
for i in range(len(second_set_of_number)):
if i != len(second_set_of_number) - 1:
temp_holder = list_to_hold_amount_of_spaces_for_bottom_operands[i] + second_set_of_number[i] + " "
temp_holder = operator_holding_list[i] + temp_holder
finalized_second_line.append(temp_holder)
else:
temp_holder = list_to_hold_amount_of_spaces_for_bottom_operands[i] + second_set_of_number[i]
temp_holder = operator_holding_list[i] + temp_holder
finalized_second_line.append(temp_holder)
# print("Second line to be printed:", finalized_second_line)
list_to_hold_dashes = []
for each in number_of_dashes:
variable = ""
for i in range(each):
variable += "-"
variable = variable + " "
list_to_hold_dashes.append(variable)
variable = ""
# print("Third line to be printed:", list_to_hold_dashes, "\n")
else:
return "Error: Too many problems."
if show_answers == False:
first_line = "".join(finalized_first_line)
second_line = "".join(finalized_second_line)
third_line = "".join(list_to_hold_dashes)
return f"{first_line}\n{second_line}\n{third_line}"
else:
for i in range(len(first_set_of_number)):
variable = ""
if operator_holding_list[i] == "+":
variable = int(first_set_of_number[i]) + int(second_set_of_number[i])
answers.append(variable)
variable = ""
if operator_holding_list[i] == "-":
variable = int(first_set_of_number[i]) - int(second_set_of_number[i])
answers.append(variable)
variable = ""
# print("Answers:", answers)
answers_to_be_printed = []
for i in range(len(answers)):
spaces_needed = number_of_dashes[i] - len(str(answers[i]))
variable = ""
for each in range(spaces_needed):
variable += " "
if i != len(answers) - 1:
variable = variable + str(answers[i]) + " "
answers_to_be_printed.append(variable)
else:
variable = variable + str(answers[i])
answers_to_be_printed.append(variable)
variable = ""
# print("Fourth conditional line to be printed:", answers_to_be_printed)
first_line = "".join(finalized_first_line)
second_line = "".join(finalized_second_line)
third_line = "".join(list_to_hold_dashes)
fourth_line = "".join(answers_to_be_printed)
return f"{first_line}\n{second_line}\n{third_line}\n{fourth_line}"
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