Hello!
I have been trying to code the arithmetic formatter project and, altough I think I have a solution that fulfills the required rules, my code doesn’t pass through the test modules. I don’t understand what’s happening… do I have to rewrite the output for the function? What am I missing?
Here is my code,
def arithmetic_arranger(problems,result=False):
problem_list = problems
# Start i variable for first time to add spaces
i = 1
line1total = ""
line2total = ""
line3total = ""
line4total = ""
# Checking for errors
# Error type 1 - more than 5 problems
problem_qty = len(problem_list)
if len(problem_list) > 5:
print("Error: Too many problems.")
exit
# Error type 2 - operators different from + or -
# Scan through each list element
for element in problem_list:
# Check if +/- operators are present in each list element and retrieve the position of +/-
if "+" in element:
operator_position = element.find("+")
operator = "+"
elif "-" in element:
operator_position = element.find("-")
operator = "-"
else:
print("Error: Operator must be '+' or '-'.")
break
# Error type 3 - operands don't contain only digits
# Retrieve left and right operands
left_operand = (element[0:operator_position]).rstrip()
right_operand = (element[operator_position + 1:]).lstrip()
# Check if operands contain only digits
if left_operand.isdecimal() == False or right_operand.isdecimal() == False:
print("Error: Numbers must only contain digits.")
break
# Error type 4 - operands must be maximum 4 digits long
if len(left_operand) > 4 or len(right_operand) > 4:
print("Error: Numbers cannot be more than four digits.")
break
# Arrange problems according to formatting rules
# Determine how many spaces to add according to the longest operand
if len(left_operand) != len(right_operand):
spaces_add = (abs(len(left_operand) - len(right_operand)))+1
else:
spaces_add = 1
# Create each line of text according to the formatting rules
concatenated_string = operator + " " * spaces_add + right_operand
line1 = f"{left_operand: >{max(len(left_operand), len(concatenated_string))}}"
line2 = f"{concatenated_string: >{max(len(left_operand), len(concatenated_string))}}"
line3 = "-" * (max(len(left_operand), len(concatenated_string)))
if result:
if operator == "+":
result_line4 = int(left_operand) + int(right_operand)
line4 = f"{result_line4: >{max(len(left_operand), len(concatenated_string))}}"
else:
result_line4 = int(left_operand) - int(right_operand)
line4 = f"{result_line4: >{max(len(left_operand), len(concatenated_string))}}"
if i == 1:
line1total = line1total + line1
line2total = line2total + line2
line3total = line3total + line3
if result:
line4total = line4total + line4
else:
line1total = line1total + " " + line1
line2total = line2total + " " + line2
line3total = line3total + " " + line3
if result:
line4total = line4total + " " + line4
i = i + 1
if result:
print(line1total + "\n" + line2total + "\n" + line3total + "\n" + line4total)
else:
print(line1total + "\n" + line2total + "\n" + line3total)
Thank you!
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: