Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I have completed all tests manually, and the outputs seem correct. However, I don’t understand what’s wrong.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
	
    list_operand1 = []
    list_operand2 = []
    list_result = []
    list_operator = []
    
    for problem in problems:
        problem = problem.split()
        if not problem[0].isdigit() or not problem[2].isdigit():
        	return print('Error: Numbers must only contain digits.')
    for problem in problems:
        problem = problem.split()    
        operand1 = int(problem[0])
        operand2 = int(problem[2])
        operator = problem[1]
        if len(problems) > 5:
            return print('Error: Too many problems.')
       	elif operand1 > 9999 or operand2 > 9999:
            return print('Error: Numbers cannot be more than four digits.')        
        elif show_answers == False:
        	result = " "
        elif show_answers == True and operator == '+':
            result = operand1 + operand2
        elif show_answers == True and operator == '-':
            result = operand1 - operand2
        elif operator != "+" or operator != "-":
            return print("Error: Operator must be '+' or '-'.")         
       	list_operand1.append(operand1)
        list_operand2.append(operand2)
        list_result.append(result)
        list_operator.append(operator)
    line1 = ""
    line2 = ""
    string = ""
    line3 = ""    
    for i in range(len(list_operand1)):
      loop1=len(str(list_operand1[i]))
      loop2=len(str(list_operand2[i]))
      loop3=len(str(list_result[i]))
      size = max(loop1, loop2)
      line1=line1 + " "*(2+size-loop1)+str(list_operand1[i])+ " " * 4
      line2=line2 + list_operator[i] + " "*(1 + size - loop2) + str(list_operand2[i])  + " "*4
      string=string + "-" * (size + 2) + " " * 4
      line3 =line3 + " " * (2 + size - loop3) + str(list_result[i]) + " " * 4
        
    return print(f'\n{line1}\n{line2}\n{string}\n{line3}')
   

arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Can you please share more information?

What errors are you getting?
What tests are you not passing?
What’s the output?

None of them:

But if I call function manually:


It seems like all of them should pass. I can see outputs clearly.

Tests:

  • arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ -----.

  • Failed:arithmetic_arranger(["1 + 2", "1 - 9380"]) should return 1 1\n+ 2 - 9380\n--- ------.

  • Failed:arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]) should return 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----.

  • Failed:arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]) should return 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------.

  • Failed:arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]) should return 'Error: Too many problems.'.

  • Failed:arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]) should return "Error: Operator must be '+' or '-'.".

  • Failed:arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers cannot be more than four digits.'.

  • Failed:arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers must only contain digits.'.

  • Failed:arithmetic_arranger(["3 + 855", "988 + 40"], True) should return 3 988\n+ 855 + 40\n----- -----\n 858 1028.

  • Failed:arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True) should return 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028.

Your function is failing all of the tests because it isn’t really returning the answers, it’s returning a print()

return print(f'\n{line1}\n{line2}\n{string}\n{line3}')

Don’t worry about printing the result in the function, the function should return only the result.

If you want to print it, print the function call like this:

print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))

If you need to troubleshoot more after that add repr() to see the raw string output:

print(repr(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)))

I changed my code. Now, it is passing the all error tests. But it can not pass other tests.

For the test above, output is :
image

It seems correct but I am not sure about it.
Should I get rid of the ‘\n’ or f-string on the beginning of last return?
return f’\n{line1}\n{line2}\n{string}\n{line3}’

By the way if I remove the ‘\n’, output seems like this:

if you want more help you need to post your updated code

yes, the final output doesn’t require a newline at the beginning, if you want to have it put it in your print

Just focus on passing the first test first.

Use repr() to compare your output to the expected output.

print(repr(arithmetic_arranger( ["3801 - 2", "123 + 49"])))

should return

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

This will show the spacing and the newline characters.

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return 'Error: Too many problems.'

    line1, line2, line3, line4 = "", "", "", ""
    for problem in problems:
        operand1, operator, operand2 = problem.split()

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

        if not (operand1.isdigit() and operand2.isdigit()):
            return 'Error: Numbers must only contain digits.'

        if len(operand1) > 4 or len(operand2) > 4:
            return 'Error: Numbers cannot be more than four digits.'

        width = max(len(operand1), len(operand2)) + 2

        line1 += operand1.rjust(width) + "    "
        line2 += operator + operand2.rjust(width - 1) + "    "
        line3 += '-' * width + "    "

        if show_answers:
            result = str(eval(problem))
            line4 += result.rjust(width) + "    "

    arranged_problems = "\n".join([line1.rstrip(), line2.rstrip(), line3.rstrip(), line4.rstrip()])
    return arranged_problems
   

print(arithmetic_arranger(["3801 - 2", "123 + 49"]))

I changed my code like this. It is passing some of the tests, but I cannot understand what’s wrong with the code.

And I tried my code on a different platform:

It seems perfect. However, on the freeCodeCamp preview board, there are three greater-than signs (>) and it is ruining my output. Why are you including these as default?

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

AssertionError: '  3801      123\n-    2    +  49\n------    -----\n' != '  3801      123\n-    2    +  49\n------    -----'
    3801      123
  -    2    +  49
- ------    -----
?                -
+ ------    -----

you have an extra new line character at the end of last line

1 Like

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