Arithmetic _arranger

Hello everyone, I have been working on my arithmetic arranger project for days I don’t know where I got it wrong that cannot pass unit-test, I checked all the condition in my IDE it works fine but fail designated test please help;
the code:

def arithmetic_arranger(problems, display_result=False):
  upper_value = ""
  lower_value = ""
  dashes = ""
  f_result = ""

  if len(problems) > 5:
    return "Error: Too many problems."

  for problem in problems:
    problem = problem.split()
    x = problem[0]
    y = problem[2]
    operator = problem[1]

    if operator not in ['+', '-']:
      return "Error: Operator must be '+' or '-'."

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

    try:
      num_1 = int(x)
      num_2 = int(y)
    except:
      return "Error: Numbers must only contain digits."

    if operator == '+':
      result = str(num_1 + num_2)
      

    else:
      result = str(num_1 - num_2)
     

    space = max(len(x), len(y)) + 2
    first = x.rjust(space)
    second= str(operator + y.rjust(space - 1))
    dash = str('-' * len(result)).rjust(space)
    result = result.rjust(space)

    if problem == problems[-1]:
      upper_value += first
      lower_value += second
      dashes += dash
      f_result += result
      
    else:
      upper_value += first + '    '
      lower_value += second + '    '
      dashes += dash + '    '
      f_result += result + '    '

  if display_result:
    arranger = upper_value + '\n' + lower_value + '\n' + dashes + '\n' + f_result
  else:
    arranger = upper_value + '\n' + lower_value + '\n' + dashes

  return arranger

console response it too long please check the link below

----------------------------------------------------------------------
Ran 6 tests in 0.039s

FAILED (failures=2)
 ^C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 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 (’).

Please provide either a link to your project on repl.it or at least run the code and copy the error message.

2 Likes

Hi,
@Abdulwaduud

You are not far away from the solution!

  1. I have removed the str from second and dash and adjusted a little.
  2. You have always space at the end of every operation line. Use rstrip() to remove it and you have the solution.
  3. I have adjusted your code according to 1) and 2) and it runs!
2 Likes

Try printing out ‘x’ instead of a space.
This helped me understand why my answer seemed correct but wasn’t.
I faced a similar issue where spaces were printed at the end of each line but I couldn’t see it initially :slight_smile:

2 Likes

help me check at

boilerplate-arithmetic-formatter-3 - Replit

https://repl.it/@JAMIUWADUD/boilerplate-arithmetic-formatter-3#arithmetic_arranger.py 2

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