Scientific Computing with Python Projects - Arithmetic Formatter

I’ve created a solution to this problem which seems to always produce the correct output for me when ran in a seperate replit. However, whenever I run the tests on the replit you have provided it fails the tests. If anyone can explain why this would be appreciated.

Your code so far

edit: for some reason it won’t post my code with the indentations but I believe there are no indentation issues

def arithmetic_arranger(problems, show_answers=True):
  if len(problems) > 5:
    return "Error: Too many problems."
  # create lines for solution
  line1 = f''
  line2 = f''
  line3 = f''
  line4 = f''
  count = 0
  # find where to split the string and value of the operator
  for problem in problems:
    count += 1
    index = problem.find(' ')
    operator = problem[index + 1]
    first_number = problem[0:index]
    second_number = problem[index + 3:]
    # determine if solution requires addition or subtraction
    if operator == "+":
      sol = int(first_number) + int(second_number)
    elif operator == "-":
      sol = int(first_number) - int(second_number)
    else:
      return "Error: Operator must be '+' or '-'."
    if first_number.isdigit():
      pass
    else:
      return "Error: Numbers must only contain digits."
    if second_number.isdigit():
      pass
    else:
      return "Error: Numbers must only contain digits."
    if len(first_number) > 4:
      return "Error: Numbers cannot be more than four digits."
    else:
      pass
    if len(second_number) > 4:
      return "Error: Numbers cannot be more than four digits."
    else:
      pass
    # create strings to be added to solution lines
    x = ''
    y = ''
    z = ''
    s = ''
    # find how many spaces to add to each item to make soluution right aligned
    if len(first_number) >= len(second_number):
      length_of_solution = len(first_number) + 2
    else:
      length_of_solution = len(second_number) + 2
    number_of_times_to_add_space1 = length_of_solution - len(first_number)
    number_of_times_to_add_space2 = length_of_solution - len(second_number) -1
    number_of_times_to_add_space3 = length_of_solution - len(str(sol))
    # add the required spaces and then determine if its the last problem to decide if there needs to be another four spaces to seperate the problems
    if count < len(problems):
      for i in range(number_of_times_to_add_space1):
        x = ' ' + x
      x = x + first_number
      for i in range(number_of_times_to_add_space2):
        y = ' ' + y
      y = operator + y + second_number
      for i in range(number_of_times_to_add_space3):
        s = ' ' + s
      s = s + str(sol)
      line1 = line1 + x + '    '
      line2 = line2 + y + '    '
      line4 = line4 + s + '    '
      for i in range(length_of_solution):
        z = '-' + z
      line3 = line3 + z + '    '
    else:
      for i in range(number_of_times_to_add_space1):
        x = ' ' + x
      x = x + first_number
      for i in range(number_of_times_to_add_space2):
        y = ' ' + y
      y = operator + y + second_number
      for i in range(number_of_times_to_add_space3):
        s = ' ' + s
      s = s + str(sol)
      line1 = line1 + x
      line2 = line2 + y
      line4 = line4 + s
      # create dashed line
      for i in range(length_of_solution):
        z = '-' + z
      line3 = line3 + z
      # create final answer
  arranged_problems = f'{line1}\n{line2}\n{line3}\n{line4}'
  return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Can you please post the error messages? They usually explain what the problem is (and it’s usually extra spaces at the end of your lines or somewhere)

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

I’ve edited your code 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 (').

Hi thanks for your reply.
I can’t copy and paste the error code for some reason but the assertion error given is something like:

    1         1
  + 2    - 9380
- ---    ------
+ ---    ------
?              +
+   3     -9379

If I am correct this means that there is a space after the dashed lines. However when I add the word test to the end of the dashed line in the variable passed to the return call at the end (arranged_problems = f’{line1}\n{line2}\n{line3}test\n{line4}') it gives that line as:

- ---    ------test

This shows that there is no space and so I am confused. If you could help further that would be appreciated.

Edit:
I’ve also tried using line3 = line3.rstrip() but that hasn’t solved it

You’re likely correct, there is a space there. Can you show this type of error line as well, the assert?

You might have to right-click/copy. You can do it, it’s just kind of fiddly the way it handles it.

Hi,
Thank you for your help. I’ve now got it to pass all 10 tests :muscle: :muscle: :muscle:

1 Like

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