Problem with Arithmetic Formatter / Python

Tell us what’s happening:
Well, I’m only at the first project and I got stuck. I feel like the code works since the output is correct. But then I am presented with many errors that I don’t understand.

Your code so far

def arithmetic_arranger(problems, displayed):
    number_of_problems = len(problems)
    first_lines = ""
    second_lines = ""
    operator = ""
    desired_sums = ""

    fin_firsts = ""
    fin_seconds = ""
    fin_lines = ""
    fin_sums = ""
    
    if number_of_problems > 5:
      return "Error: Too many problems."

    for i in problems:
      split_problems = i.split()
      first_lines = split_problems[0]
      second_lines = split_problems[2]
      operator = split_problems[1]

      if (not first_lines.isnumeric()) or (not second_lines.isnumeric()):
        return "Error: Numbers must only contain digits."

      if len(first_lines)>4 or len(second_lines)>4:
        return "Error: Numbers cannot be more than four digits."

      if ((operator == "+") or (operator == "-")):
            
        if operator == "+":
          desired_sums = str(int(first_lines) + int(second_lines))
        else:
          desired_sums = str(int(first_lines) - int(second_lines))


        if (len(first_lines)>len(second_lines)):
          lines_length = len(first_lines) + 2
        else:
          lines_length = len(second_lines) + 2

        top_line = str(first_lines).rjust(lines_length)
        bottom_line = operator + str(second_lines).rjust(lines_length - 1)
        result = str(desired_sums).rjust(lines_length)

        small_lines = ""

        for j in range(lines_length):
          small_lines += "-"

        fin_firsts += top_line + "    "
        fin_seconds += bottom_line + "    "
        fin_lines += small_lines + "    "
        fin_sums += result + "    "

      else:
        return "Error: Operator must be '+' or '-'."

    fin_firsts.rstrip()
    fin_seconds.rstrip()
    fin_lines.rstrip()
    fin_sums.rstrip()

    if displayed:
      arranged_problems = fin_firsts + "\n" + fin_seconds + "\n" + fin_lines + "\n" + fin_sums
    else:
      arranged_problems = fin_firsts + "\n" + fin_seconds + "\n" + fin_lines

    return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Arithmetic Formatter

Link to the challenge:

What errors does the test suite provide?

Based on this, I’d guess you have extra spaces at the end of your lines.


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 (').

Oh, thanks
Wait a minute I’m copying the Errors

Ah, so in addition to adding extra spaces onto the end of your lines

This should be an optional argument.

Ohh, I see. But still does this have anything to do with the final error? The one that says FAIL?

That error is related to this:

But the project asks for 4 spaces after the lines
and everything left I’ve stripped

Sorry for bugging you with questions
Thanks for replying so fast and helping me

It should be 4 spaces in between problems and 0 spaces at the end of a line.

Ooohh, so I have to think about the last problem
Thanks a lot

Yeah, there a few ways to tackle it, so it’s a question of which feels natural to you.

Feel free to ask more follow-ups if they come up.

So, I’ll try to post the new code with the backticks

def arithmetic_arranger(problems, *displayed):
    number_of_problems = len(problems)
    first_lines = ""
    second_lines = ""
    operator = ""
    desired_sums = ""

    fin_firsts = ""
    fin_seconds = ""
    fin_lines = ""
    fin_sums = ""
    
    if number_of_problems > 5:
      return "Error: Too many problems."

    for i in problems:
      split_problems = i.split()
      first_lines = split_problems[0]
      second_lines = split_problems[2]
      operator = split_problems[1]

      if (not first_lines.isnumeric()) or (not second_lines.isnumeric()):
        return "Error: Numbers must only contain digits."

      if len(first_lines)>4 or len(second_lines)>4:
        return "Error: Numbers cannot be more than four digits."

      if ((operator == "+") or (operator == "-")):
            
        if operator == "+":
          desired_sums = str(int(first_lines) + int(second_lines))
        else:
          desired_sums = str(int(first_lines) - int(second_lines))


        if (len(first_lines)>len(second_lines)):
          lines_length = len(first_lines) + 2
        else:
          lines_length = len(second_lines) + 2

        top_line = str(first_lines).rjust(lines_length)
        bottom_line = operator + str(second_lines).rjust(lines_length - 1)
        result = str(desired_sums).rjust(lines_length)

        small_lines = ""

        for j in range(lines_length):
          small_lines += "-"
        if i == len(problems):
          fin_firsts += top_line
          fin_seconds += bottom_line
          fin_lines += small_lines
          fin_sums += result
        else:
          fin_firsts += top_line + "    "
          fin_seconds += bottom_line + "    "
          fin_lines += small_lines + "    "
          fin_sums += result + "    "

      else:
        return "Error: Operator must be '+' or '-'."

    fin_firsts.rstrip()
    fin_seconds.rstrip()
    fin_lines.rstrip()
    fin_sums.rstrip()

    if displayed:
      arranged_problems = fin_firsts + "\n" + fin_seconds + "\n" + fin_lines + "\n" + fin_sums
    else:
      arranged_problems = fin_firsts + "\n" + fin_seconds + "\n" + fin_lines

    return arranged_problems

but now emerges a different problem:

I don’t get it. The output is identical to what the project asks.

I GOT IT. I’m just dumb. rstrip works, but only if you assign it to something. I’m really dumb. Thanks Jeremy.

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