Stuck on the Arithmetic Formatter - Python

Having a bit of trouble with this first project. It’s my first experience with Repl.it, so I could be simply doing something wrong.

I coded the program in Sublime, and had it working well enough to pass all of the tests that I put it through on there, and then pasted it into Repl.it. I had to repair a bunch of formating errors before the code would run.

Then it would throw test errors for all kinds of stuff, and I would change things around until it worked. After a number of hours, I still fail 4 of the 6 tests, so I would like some guidance on what I might be doing wrong.

Cheers

def arithmetic_arranger(problems, showResult=False):
    #Check if there are more than 5 problems
    if len(problems) > 5:
        return ('Error: Too many problems.')
#Split problems into list_of_questions
    list_of_questions = []
    for item in problems:
        #Check that the operator is - or +
        list_of_questions.append(item.split())

#Check the numbers do no have more than 4 digits
    for item in list_of_questions:
      #Make empty lists that will hold the final data
      line1 = []
      line2 = []
      line3 = []
      line4 = []
      for item in list_of_questions:
          #Remove nondigits
          if not int(len(item[0])) <= 4 or not int(len(item[2])) <= 4:
              print('Error: Numbers cannot be more than four digits')
              return ('Error: Numbers cannot be more than four digits')
          if not item[0].isnumeric() or not item[2].isnumeric():
              print("Error: Numbers must only contain digits.")
              return("Error: Numbers must only contain digits.")
          #Check that the operator is valid
          if item[1] == '+' or item[1] == '-':
            pass
          else:
            print('Error: Operator must be '+' or '-'.')
            return ('Error: Operator must be '+' or '-'.')
          #Find the answer
          if item[1] == "+":
              answer = int(item[0]) + int(item[2])
          else:
              answer = int(item[0]) - int(item[2])
          #Define which of the numbers is longest, and format accordingly.
          if len(item[0]) >= len(item[2]):
              longest = int(len(item[0]))
              shortest = int(len(item[2]))
              line2.append(item[1] + (' ' * (longest + 1 - shortest) + item[2] + '    '))
              line1.append(' ' * (longest + 2 - longest) + item[0] + '    ')
              line3.append('-' * (longest + 2) + '    ')
              if showResult == True:
                  line4.append('{0: >{1}}{2}'.format(answer, (longest + 2), '    '))
          else:
              longest = int(len(item[2]))
              shortest = int(len(item[0]))
              line2.append(item[1] + (' ' * (longest + 1 - longest) + item[2] + '    '))
              line1.append(' ' * (longest + 2 - shortest) + item[0] + '    ')
              line3.append('-' * (longest + 2) + '    ')
              if showResult == True:
                  line4.append('{0: >{1}}{2}'.format(answer, (longest + 2), '    '))
          if showResult:
            arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n' + str(line4) + '\n'
          else:
            arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n'
    return(arranged_problems)

can you provide the link to your repl?

Certainly! Here you go!

this is your output

['   32    ', '  3801    ', '  45    ', '  123    ']
['+ 698    ', '-    2    ', '+ 43    ', '+  49    ']
['-----    ', '------    ', '----    ', '-----    ']

it should be

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

when you do this:

          if showResult:            
            arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n' + str(line4) + '\n'
          else:
            arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n'

line1, line2 etc are lists, using str to convert them will keep also the comma between elements and the square parenthesis - use a different method


for this:

          else:
            print('Error: Operator must be '+' or '-'.')
            return ('Error: Operator must be '+' or '-'.')

you get error
TypeError: unsupported operand type(s) for -: 'str' and 'str'

because the + and - have ended between strings

Thank you for your help! I converted the integers in my list to strings and then used the join method with the separator being the required four spaces.

first = [str(i) for i in line1]
second = [str(i) for i in line2]
third = [str(i) for i in line3]
fourth = [str(i) for i in line4]
if showResult:
    arranged_problems = '    '.join(first) + '\n' + '    '.join(second) + '\n' + '    '.join(third) + '\n' + '    '.join(fourth)
else:
    arranged_problems = '    '.join(first) + '\n' + '    '.join(second) + '\n' + '    '.join(third)

The answer now prints correctly. :slightly_smiling_face:

For the erroneous return string, I used both double quotes and single quotes to solve the issue.

return "Error: Operator must be '+' or '-'."

Everything passes now. Thanks again for your help!

1 Like