Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
I am unsure why my code for the Arithmetic Formatter is not working. The output console appears to show that they are correct, but the test says the code is not correct. This is what the console for first test says:

===================== FAILURES =====================
__ test_template[test_two_problems_arrangement1] ___

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
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           -    2    +  49
E         - ------    -----
E         + ------    -----
E         ?                +

*Because of the difference between the size of the plus and minus on this forum, it looks like they are offset, but in my console they look exactly identical.

**Your code so far**
def arithmetic_arranger(problems):
  problem1=[]
  problem2=[]
  problem3=[]
  problem4=[]
  problem5=[]
  n = 0
  for problem in problems:
    n = n + 1
  
  if n >= 1:
    problem1 = problems[0]
    problem1 = problem1.split()
    space1 = len(problem1[0]) - len(problem1[2])
    str1a = problem1[0]
    str1b = problem1[1] + ' ' + ' '*space1 + problem1[2]
    max_str1 = max(len(str1a), len(str1b))
    str1a = ' ' * (max_str1 - len(str1a)) + str1a
    str1b = ' ' * (max_str1 - len(str1b)) + str1b
    strA = str1a
    strB = str1b
    strC = '-'*len(str1b)
  if n >= 2:
    problem2 = problems[1]
    problem2 = problem2.split()
    space2 = len(problem2[0]) - len(problem2[2])
    str2a = problem2[0]
    str2b = problem2[1] + ' ' + ' '*space2 + problem2[2]
    max_str2 = max(len(str2a), len(str2b))
    str2a = ' ' * (max_str2 - len(str2a)) + str2a
    str2b = ' ' * (max_str2 - len(str2b)) + str2b
    strA = strA + ' '*4 + str2a
    strB = strB + ' '*4 + str2b
    strC = strC + ' '*4 + '-'*len(str2b)
  if n >= 3:
    problem3 = problems[2]
    problem3 = problem3.split()
    space3 = len(problem3[0]) - len(problem3[2])
    str3a = problem3[0]
    str3b = problem3[1] + ' ' + ' '*space3 + problem3[2]
    max_str3 = max(len(str3a), len(str3b))
    str3a = ' ' * (max_str3 - len(str3a)) + str3a
    str3b = ' ' * (max_str3 - len(str3b)) + str3b
    strA = strA + ' '*4 + str3a
    strB = strB + ' '*4 + str3b
    strC = strC + ' '*4 + '-'*len(str3b)
  if n >= 4:
    problem4 = problems[3]
    problem4 = problem4.split()
    space4 = len(problem4[0]) - len(problem4[2])
    str4a = problem4[0]
    str4b = problem4[1] + ' ' + ' '*space4 + problem4[2]
    max_str4 = max(len(str4a), len(str4b))
    str4a = ' ' * (max_str4 - len(str4a)) + str4a
    str4b = ' ' * (max_str4 - len(str4b)) + str4b
    strA = strA + ' '*4 + str4a
    strB = strB + ' '*4 + str4b
    strC = strC + ' '*4 + '-'*len(str4b)
  if n >= 5:
    problem5 = problems[4]
    problem5 = problem5.split()
    space5 = len(problem5[0]) - len(problem5[2])
    str5a = problem5[0]
    str5b = problem5[1] + ' ' + ' '*space5 + problem5[2]
    max_str5 = max(len(str5a), len(str5b))
    str5a = ' ' * (max_str5 - len(str5a)) + str5a
    str5b = ' ' * (max_str5 - len(str5b)) + str5b
    strA = strA + ' '*4 + str5a
    strB = strB + ' '*4 + str5b
    strC = strC + ' '*4 + '-'*len(str5b)
  if n >= 6:
    print("Error: Too many problems.")
    exit()
  strA = strA + '\n'
  strB = strB + '\n'
  strC = strC + '\n'
  #print(strA)
  #print(strB)
  #print(strC)
  arranged_problems = strA + strB + strC
  #return print(f"{strA}\n{strB}\n{strC}\n")
  return arranged_problems

#The reason the commented out ‘return’ function is in the code is because I tried #removing the ‘\n’ from the strings above, and using a different method to get the #correct string, but it still did not work.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Okay, I fixed it! Thank you very much!

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