Scientific Computing with Python- Arithmetic Formatter

Hi, I am very new to coding and am struggling to understand my error messages in replit. The code works when I work on it when I haven’t defined it and am working on a single equation, but when I define it and put it onto replit the equation parts are not working and I’m not entirely sure why. Sorry if it’s simple, I am still new to this. Thanks in advance!

Here is the code:

def arithmetic_arranger(problems, solve = False):
  
  if len(problems) > 5:
      return "Error: Too many problems."
  
  for problem in problems:
      peices = problem.split()
    # print(peices)

      firstNumber = peices[0]
    
    # check that firstNumber is only numbers
      try: 
          int(firstNumber)
      except: 
          return "Error: Numbers must only contain digits."
          continue
   
    #check that firstNumber is not more than four digits long
      if len(firstNumber) > 4:
          return "Error: Numbers cannot be more than four digits."
    # print(firstNumber)
    
   
      operator = peices[1]
    # print(operator)
      if not "+" or "-" in operator:
          return "Error: Operator must be '+' or '-'."
    
      secondNumber = peices[2]

    # check that secondNumber is only numbers
      try: 
          int(secondNumber)
      except: 
          return "Error: Numbers must only contain digits."
          continue
        
    
    #check that secondNumber is not more than four digits long
      if len(secondNumber) > 4:
          return "Error: Numbers cannot be more than four digits."
    
    # print(secondNumber)


# you need to find a way to change the print errors to return errors

# now doing the equation

  firstLine = []
  secondLine = []
  thirdLine = []
  fourthLine = []

  for firstPosition in problems:
      if len(firstNumber) > len(secondNumber):
          firstLine.append(" "*2 + firstNumber)
      else:
          firstLine.append(" "*(len(secondNumber)-len(firstNumber) + 2)+ firstNumber)
# print(firstLine)



  for secPosition in problems:
    
      if len(firstNumber) > len(secondNumber):
          secondLine.append(operator + " "*(len(firstNumber)-len(secondNumber) + 1) + secondNumber)
      else:
          secondLine.append(operator + " " + secondNumber)
# print(secondLine)

  for dashes in problems:
      if len(firstNumber) > len(secondNumber): 
          thirdLine.append("-"*(len(firstNumber) + 2))
      else: 
          thirdLine.append("-"*(len(secondNumber) + 2))
# print(thirdLine)


  for solution in problems:
      if "+" in operator: 
          answer = int(firstNumber) + int(secondNumber)
      else:
          answer = int(firstNumber) - int(secondNumber)

  answer = str(answer)


  for ansPosition in problems: 
      if len(firstNumber) > len(secondNumber):
          if len(firstNumber) > len(answer):
              fourthLine.append(" "*(len(firstNumber)-len(answer)) + answer)
          elif len(firstNumber) == len(answer):
              fourthLine.append(" "*2 + answer)
          else: 
              fourthLine.append(" "*(len(answer)-len(firstNumber)) + answer)
      else: 
          if len(secondNumber) > len(answer):
              fourthLine.append(" "*(len(secondNumber)-len(answer)) + answer)
          elif len(secondNumber) == len(answer):
              fourthLine.append(" "*2 + answer)
          else: 
              fourthLine.append(" "*(len(answer)-len(secondNumber)) + answer)

# print(fourthLine)

  arranged_problems = " ".join(firstLine) + "\n" + " ".join(secondLine) + "\n" + " ".join(thirdLine) + "\n" + " ".join(fourthLine)

  print(arranged_problems)

  return arranged_problems

Link to the code:

https://replit.com/@cayharris/boilerplate-arithmetic-formatter-6#arithmetic_arranger.py

hi there, welcome to the forum.

please post a link to your replit by copying the URL within 2 angled brackets <URL>

Also you can copy the error messages from replit and post them here and we can help you understand them as well.

Edit: I just edited your post to make the replit link visible btw.

Try to edit your main.py to comment out the automated tests. This will allow you to slowly test your code until you believe it is in good shape and then run the automated tests when you are ready.
For eg. in main.py you have this right now

# This entrypoint file to be used in development. Start by reading README.md
from pytest import main

from arithmetic_arranger import arithmetic_arranger


print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))


# Run unit tests automatically
main(['-vv'])

If you comment out the line main(['vv'])
and run, you will see this output:

 python main.py
Error: Operator must be '+' or '-'.
 

So clearly this is the wrong output given that the input is
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))

So start by debugging this and let us know if you need help with this and or further debugging.

1 Like

Hi, thank you so much! Will do that now, still learning to use the forum!

1 Like

Hi there!

Thanks for the advice- commenting out the automated tests made it a lot easier to work on it.

I’ve now got it to a point where it is ALMOST working: it prints out all the equations as I split them, and it prints the final output in the way I wanted it formatted. The only problem is printing it in the final format only seems to loop through the last equation and I cannot figure out why!

def arithmetic_arranger(problems, solve = False):

  if len(problems) > 5:
      return "Error: Too many problems."
  
  for problem in problems:
      peices = problem.split()
      print(peices)

      firstNumber = peices[0]
      operator = peices[1]
      secondNumber = peices[2]

      print(firstNumber)
      print(operator)
      print(secondNumber)
    
    # check that firstNumber is only numbers
      try: 
          int(firstNumber)
      except: 
          return "Error: Numbers must only contain digits."
          continue
   
    #check that firstNumber is not more than four digits long
      if len(firstNumber) > 4:
          return "Error: Numbers cannot be more than four digits."
    # print(firstNumber)
    
   
      # print(operator)
      if operator == "+"or "-":
          continue
      else:
          return "Error: Operator must be '+' or '-'."
    
      

    # check that secondNumber is only numbers
      try: 
          int(secondNumber)
      except: 
          return "Error: Numbers must only contain digits."
          
        
    
    #check that secondNumber is not more than four digits long
      if len(secondNumber) > 4:
          return "Error: Numbers cannot be more than four digits."
    
    # print(secondNumber)


# you need to find a way to change the print errors to return errors

# now doing the equation
  #print(secondNumber)
  firstLine = []
  secondLine = []
  thirdLine = []
  fourthLine = []

  for i in problems:
      if len(firstNumber) > len(secondNumber):
          firstLine.append(" "*2 + firstNumber)
      else:
          firstLine.append(" "*(len(secondNumber)-len(firstNumber) + 2)+ firstNumber)
  



  for secPosition in problems:
    
      if len(firstNumber) > len(secondNumber):
          secondLine.append(operator + " "*(len(firstNumber)-len(secondNumber) + 1) + secondNumber)
      else:
          secondLine.append(operator + " " + secondNumber)
# print(secondLine)

  for dashes in problems:
      if len(firstNumber) > len(secondNumber): 
          thirdLine.append("-"*(len(firstNumber) + 2))
      else: 
          thirdLine.append("-"*(len(secondNumber) + 2))
# print(thirdLine)


  for solution in problems:
      if "+" in operator: 
          answer = int(firstNumber) + int(secondNumber)
      else:
          answer = int(firstNumber) - int(secondNumber)

  answer = str(answer)


  for ansPosition in problems: 
      if len(firstNumber) > len(secondNumber):
          if len(firstNumber) > len(answer):
              fourthLine.append(" "*(len(firstNumber)-len(answer)) + answer)
          elif len(firstNumber) == len(answer):
              fourthLine.append(" "*2 + answer)
          else: 
              fourthLine.append(" "*(len(answer)-len(firstNumber)) + answer)
      else: 
          if len(secondNumber) > len(answer):
              fourthLine.append(" "*(len(secondNumber)-len(answer)) + answer)
          elif len(secondNumber) == len(answer):
              fourthLine.append(" "*2 + answer)
          else: 
              fourthLine.append(" "*(len(answer)-len(secondNumber)) + answer)

# print(fourthLine)

  arranged_problems = " ".join(firstLine) + "\n" + " ".join(secondLine) + "\n" + " ".join(thirdLine) + "\n" + " ".join(fourthLine)

  #print(arranged_problems)

  return arranged_problems

Here is the output:

 python main.py
['32', '+', '698']
32
+
698
['3801', '-', '2']
3801
-
2
['45', '+', '43']
45
+
43
['123', '+', '49']
123
+
49
  123   123   123   123
+  49 +  49 +  49 +  49
----- ----- ----- -----
  172   172   172   172

here is the link to the project:

https://replit.com/@cayharris/boilerplate-arithmetic-formatter-6#arithmetic_arranger.py

hi there, just a quick comment, I noticed you are using print and actually you should not be doing that. You should be creating a string and returning it which when printed later displays the expected output.

the reason is that in the first for loop you wrote, you looped through the equations and the last thing you did was look at the last equation therefore firstNumber is the first number of the last equation.
Then later, after that first for loop ended, you started another for loop

and at this point firstNumber is set to the last first number you saw in the previous loop.

1 Like

Hey there, thank you so much again for the help. I managed to fix my loops so that all the answers were put into the second loop and the output now seems to be exactly what they were looking for. Here is the output:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
  730      3799      88      172

which seems to be exactly how they want it to be. However, when I remove the comments in main.py to allow the tests to run, it says 9/10 of the tests have failed. This is including the four tests (the ‘‘Error: Too many problems.’’, ‘‘Error: Operator must be ‘+’ or ‘-’.’’, ‘’ Error: Numbers must only contain digits.‘’ and ‘’ Error: Numbers cannot be more than four digits.‘’ ) which I had originally passed and dont think I made any changes to this part of the code. Not sure why it is failing when the output is what they want?

Again, thank you very much in advance

Link to current state of code:

https://replit.com/@cayharris/boilerplate-arithmetic-formatter-6#main.py

in this code block

              fourthLine.append(" "*(len(answer[j])-len(secondNumber[j])) + answer[j])            

you are trying to get the len of an int according to the error message being received which says:
E TypeError: object of type ‘int’ has no len()

So check which of these variables is an int as you cannot take the len of an int

Thank you! This also helped me understand all the other error messages better and I figured out what they were asking for. I managed to fix 10/10 parameters and have now passed the arithmetic formatter challenge! Thank you so much for all your help!

1 Like

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