Scientific Computing with Python Projects - Arithmetic Formatter

Please help with my code. I don’t know where the problem lies after searching for hours. I keep getting this;
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]

Your code so far

 def arithmetic_arranger(problems, solver = False):
  #1st part
  #take second argument which is set to true
  #problems should be less than 5. else Error: Too many problems.
  #addition and subtraction. Else Error: Operator must be '+' or '-'.
  #only numbers. else Error: Numbers must only contain digits.
  #only 4 digits in wideth. else Error: Numbers cannot be more than four digits.

  #split problems into singles
  if len(problems) > 5:
    return "Error: Too many problems."
  
  up=""
  down=""
  lines=""
  ans=""
  final=""
 
  for problem in problems:
    ty=problem.split(" ")
    firstnum=ty[0]
    operator=ty[1]
    secondnum=ty[2]
  
    if len(firstnum) >= 5:
      return "Error: Numbers cannot be more than four digits."
    elif len(secondnum) >= 5:
      return "Error: Numbers cannot be more than four digits."
      
    if firstnum.isdigit() == False:
      return "Error: Numbers must only contain digits."
    elif secondnum.isdigit() == False:
      return "Error: Numbers must only contain digits."

    solu=""
    if (operator == "+"):
      solu = str(int(firstnum) + int(secondnum))
    elif (operator == "-"):
      solu = str(int(firstnum) - int(secondnum))    
    elif (operator == "*" or operator == "/"):
      return "Error: Operator must be '+' or '-'."
      
  #2nd part
 
    length= max(len(firstnum), len(secondnum)) + 2
    #print(length)
    top=str(firstnum).rjust(length)
    #print(top)
    bottom=operator + str(secondnum).rjust(length - 1)
    #print(bottom)
    answer=str(solu).rjust(length)
    #print(answer)
    dashes = ""
    for dash in range(length):
      dashes += "-"
  
    if problem != problems[-1]:
      up += top + "    "
      down += bottom + "    "
      lines += dashes + "    "
      ans += answer + "    "
    else:
      up += top
      down += bottom 
      lines += dashes
      ans += answer

  if solver:
    final = up + "\n" + down + "\n" + lines + "\n" + ans
  else:
    final = up + "\n" + down + "\n" + lines 
    return final

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Are you hosting your project on replit? If you could post a link to it, it would make it much easier for others to help troubleshoot the problem.

Yes, I did. Thank you. Please how do I do that?

Just paste the url to your project - it looks like replit.com/@username/projectname

<boilerplate-arithmetic-formatter - Replit>

Thank you. Just did that

Where did you paste the link?

1 Like

I edited your post for readability. You must place the replit link inside angled brackets to make it show up.

1 Like

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

1 Like

I think I got it now. Thanks

<https://replit.com/@Bifyx/boilerplate-arithmetic-formatter>

Thank you, I just pasted it now. Please help check the code

You don’t return anything if solver is true, other than that well done:

AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E   assert None == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')

(Indentation matters :sweat_smile: )

1 Like

Thank you. It worked

1 Like

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