Tell us what’s happening:
Yesterday I coded for that challenge and I couldn’t grasp the error and why I can’t pass the test, it seems like something about the formatting of the output, a space or other thing, the test error template is so damn cryptic…
I’m kinda frustated, could anyone help me?
Your code so far
def arithmetic_arranger(problems, boo=True):
line1 = ""
line2 = ""
line3 = ""
line4 = ""
#First error control, will return if there are more than 5 problems
if len(problems) > 5:
return "Error: Too many problems."
#Reading every arithmetic equation in problems
for arith in problems:
#Splitting operands and operator
words = arith.split()
length = 0
#Discovering the max length of the operands
for word in words:
if len(word) > length:
length = len(word)
#Second error control, limit the operand lenght to 4
if length > 4:
return "Error: Numbers cannot be more than four digits."
#It's the max length + 2 (one space of the operator plus one blank space)
length += 2
#loop to add blank spaces to line1 before the first operand
for x in range(0, length - len(words[0])):
line1 += " "
#appending first operand
line1 += words[0]
#loop to add blank spaces after operand
for x in range(0, 4):
line1 += " "
#appending operator
line2 += words[1]
#loop to add blank spaces between operator and second operand
for x in range(0, length - len(words[2]) - 1):
line2 += " "
#appending second operand
line2 += words[2]
#loop to add blank spaces after second operand
for x in range(0, 4):
line2 += " "
#dashes loop
for x in range(0,length):
line3 += "-"
#dashes blank space loop
for x in range(0, 4):
line3 += " "
#error control for the integer conversion of the two operands
try:
y = int(words[0])
z = int(words[2])
except:
return "Error: Numbers must only contain digits."
#calculation and string conversion
if words[1] == "+":
result = str(y + z)
elif words[1] == "-":
result = str(y - z)
else:
return "Error: Operator must be '+' or '-'."
#blank spaces befor result append loop
for x in range(0, length - len(result)):
line4 += " "
#append to line4 of the calculation result
line4 += result
#loop for the blank spaces after result in line4
for x in range(0, 4):
line4 += " "
#return of the final string with the strip of blank spaces at the end of each line.
return line1.rstrip() + "\n" + line2.rstrip() + "\n" + line3.rstrip() +"\n" + line4.rstrip()
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: