Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
When I print out my solution it is perfect, but it doesn’t pass the tests and I can’t understand why.

Your code so far

def arithmetic_arranger(problems, include_answer=False):
  pr=[]
  sol=0
  linum=''
  stone=''
  stwo=''
  sthree=''
  stour=''
  arranged_problems=''
  for probl in problems:
    if probl.islower() or probl.isupper():
        return ('Error: Numbers must only contain digits.')
        break
  if len(problems)>5:
    return ('Error: Too many problems.') 
  else:
    for prob in problems:
      pr=prob.split()
      if len(pr[0])>4 or len(pr[2])>4:
        return ('Error: Numbers cannot be more than four digits.') 
        break
      elif pr[1]!= '+' and pr[1]!='-':
        return('Error: Operator must be \'+\' or \'-\'.')
        break
    for prob in problems:
        pr=prob.split()
        if len(pr[0])>len(pr[2]):
            linum='-'*(len(pr[0])+2)
        else:
            linum='-'*(len(pr[2])+2)
        if prob is problems[-1]:
            stone+=((' '*(len(linum)-len(pr[0])))+pr[0]+'    ')
        else:
            stone+=((' '*(len(linum)-len(pr[0])))+pr[0]+'    ')
    for prob in problems:
        pr=prob.split()
        if len(pr[0])>len(pr[2]):
            linum='-'*(len(pr[0])+2)
        else:
            linum='-'*(len(pr[2])+2)
        if prob is problems[-1]:
            stwo+= (pr[1]+(len(linum)-(len(pr[2])+1))*' '+pr[2]+'    ')
        else:
            stwo+= (pr[1]+(len(linum)-(len(pr[2])+1))*' '+pr[2]+'    ')
    for prob in problems:
        pr=prob.split()
        if len(pr[0])>len(pr[2]):
            linum='-'*(len(pr[0])+2)
        else:
            linum='-'*(len(pr[2])+2)
        if prob is problems[-1]:
            sthree+=(linum+'    ')
        else:
            sthree+=(linum+'    ')
    for prob in problems:
        pr=prob.split()
        if len(pr[0])>len(pr[2]):
            linum='-'*(len(pr[0])+2)
        else:
            linum='-'*(len(pr[2])+2)
        if pr[1]=='+':
            sol=int(pr[0])+int(pr[2])
        else:
            sol=int(pr[0])-int(pr[2])
        stour+=((len(linum)-len(str(sol)))*' '+str(sol)+'    ')
    if include_answer==True:
        arranged_problems= (stone+'\n'+stwo+'\n'+sthree+'\n'+stour)
    else:
        arranged_problems= stone+'\n'+stwo+'\n'+sthree
    return(arranged_problems)
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

It’s just occured to me that the formatting of the code I posted here is horrible, is there a way to share my .py or .ipynb file?

I’ve edited your post 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 (').

It’s easiest if you share the link to your replit so we can see the results of the test suite ourselves.

@jnaroian The tests show you what the expected return value vs. your function’s return value for each test that you fail.

For example, for the first test it shows the expected:

'  3801      123\n-    2    +  49\n------    -----'

vs. your function’s return value:

'  3801      123    \n-    2    +  49    \n------    -----    '

You can see you have extra spaces not present in the expected return value.

Thank you so much! I couldn’t tell that was the problem from the test cases. Just fixed it and passed all tests!

Good to know, thanks for the edit!