Arithmetic Formatter - Heeeeelp

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 
      y = int(words[0])
      z = int(words[2])
      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)
      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:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

From the error message it looks like you have an extra space at the end of each line.


Thank you for the responses, Jeremy.

That was what I thought too but when I copy the same code into PyCharm there is no blank space between the lines and well…I used the rstrip() method.

I have some experience doing this kind of challenges, primarly on CodeWars, that one which is not even hard is getting on my nerves LOL. I think later I will read the test module.

It’s a new-line-character.
Look at your return-statement, you add it regardless of the final line.

Reading the test module I got some insights, first of all I didn’t see the requisite that sometimes the algorithm will make the calculations and sometimes not based on the second argument, sorry, my fault.

So basically all the entire calculation line “was the extra space”.
There were other problems but I got it right now. Thanks for all!


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.