Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Assertion error when running code against tests. Even though when testing string outputs are the same as the expected outputs in the testing file.

Your code so far

def arithmetic_arranger(problems, *args):

  

  #problems = (['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'])
  number1 = 0
  number2 = 0
  operand = ""
  line1 = ""
  line2 = ""
  line3 = ""
  line4 = ""
  spaces = 0
  indent = "   "
  sumx = 0

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

  #array split into seperate numbers and operators
  for item in problems:
      item = item.split()
      number1 = item[0].strip(" ")
      number2 = item[2].strip(" ")
      operand = item[1].strip(" ")
      try:
          int(number1)
          int(number2)
      except:
          return"Error: Operator must be '+' or '-'.'"
          break
      if len(number1) > 4 or len(number2) > 4:
          return"Error: Numbers cannot be more than four digits."
          break
      if operand == "*" or operand == "/":
          return"Error: Operator must be '+' or '-'."
          break
      if  operand == '+':
          sumx = str(int(number1) + int(number2))
      elif operand == "-":
          sumx = str(int(number1) - int(number2))
      spaces = max(len(number1), len(number2))+2

      line1 += str(number1).rjust(spaces)
      line2 += str(operand + number2.rjust(spaces-1))
      line3 += ('-' * spaces)
      line4 += str(sumx).rjust(spaces)
        

      if item != problems[-1]:
          line1 += "    "
          line2 += "    "
          line3 += "    "
          line4 += "    "

      if args: 
        arranged_problems = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
      else:
        arranged_problems = line1 + "\n" + line2 + "\n" + line3
  print(arranged_problems)
 
  
  return arranged_problems

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

It is hard to guess without the test output. Can you provide the diffs?

Yes I just figured this out after replacing the indents with “aaaa”

Also the condition where I was breaking from appending " " to each string wasnt being met as I had assigned item the split value of the array straight after starting the loop.

All passed now, definately not the most elegant solution but it works!

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