Stuck with Arithmetic Formatter Code

Get error
def arithmetic_arranger(problems,need_answer=False):
first_line=’’
second_line=’’
third_line=’’
final_line=’’
if len(problems)>5:
return ‘Error: Too many problems.’
for prob in problems:
new_prob=prob.split()
first,op,second=new_prob[0],new_prob[1],new_prob[2]

  if not((op=='+') or (op=='_')):
    return "Error: Operator must be '+' or '-'."
  elif len(first)>4 or len(second) >4:
    return 'Error: Numbers cannot be more than four digits.'
  elif not(first.isdigit() or second.isdigit()):
    return 'Error: Numbers must only contain digits.'
  
  if op=='+':
    answer=int(first)+int(second)
  elif op=='-':
    answer=int(first)-int(second)

  width=max(len(first),len(second))+2
  separation='    '
  first_option=str(first.rjust(width))
  second_option=op+str(second).rjust(width-1)
  third_option=str('-'*width)
  fourth_option =str(answer).rjust(width)

if len(problems)>0:
  first_line+=first_option+separation
  second_line+=second_option+separation  
  third_line+=third_option+separation
  final_line+=fourth_option+separation

if need_answer==True:
  arranged_problems=first_line+'\n'+second_line+'\n'+third_line+'\n'+final_line
else:
  arranged_problems=first_line+'\n'+second_line+'\n'+third_line
return print(arranged_problems)
def arithmetic_arranger(problems,need_answer=False):
    first_line=''
    second_line=''
    third_line=''
    final_line=''
    if len(problems)>5:
      return 'Error: Too many problems.'
    for prob in problems:
      new_prob=prob.split()
      first,op,second=new_prob[0],new_prob[1],new_prob[2]
      
      if not((op=='+') or (op=='_')):
        return "Error: Operator must be '+' or '-'."
      elif len(first)>4 or len(second) >4:
        return 'Error: Numbers cannot be more than four digits.'
      elif not(first.isdigit() or second.isdigit()):
        return 'Error: Numbers must only contain digits.'
      
      if op=='+':
        answer=int(first)+int(second)
      elif op=='-':
        answer=int(first)-int(second)

      width=max(len(first),len(second))+2
      separation='    '
      first_option=str(first.rjust(width))
      second_option=op+str(second).rjust(width-1)
      third_option=str('-'*width)
      fourth_option =str(answer).rjust(width)

    if len(problems)>0:
      first_line+=first_option+separation
      second_line+=second_option+separation  
      third_line+=third_option+separation
      final_line+=fourth_option+separation
    
    if need_answer==True:
      arranged_problems=first_line+'\n'+second_line+'\n'+third_line+'\n'+final_line
    else:
      arranged_problems=first_line+'\n'+second_line+'\n'+third_line
    return print(arranged_problems)

Hello there.

Can you provide more information about your question?

The more information you give us, the more likely we are to be able to help.

sorry here are the errors
Error: Operator must be ‘+’ or ‘-’.
F.EF…

ERROR: test_only_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-5/test_module.py”, line 32, in test_only_digits
actual = arithmetic_arranger([“98 + 3g5”, “3801 - 2”, “45 + 43”, “123 + 49”])
File “/home/runner/boilerplate-arithmetic-formatter-5/arithmetic_arranger.py”, line 20, in arithmetic_arranger
answer=int(first)+int(second)
ValueError: invalid literal for int() with base 10: ‘3g5’

======================================================================
FAIL: test_arrangement (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-5/test_module.py”, line 10, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]’)
AssertionError: “Error: Operator must be ‘+’ or ‘-’.” != ’ 3 3801 45 123\n+ 855 [56 chars]----’

  • Error: Operator must be ‘+’ or ‘-’.
  • 3      3801      45      123
    
  • 855 - 2 + 43 + 49

: Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]

======================================================================
FAIL: test_solutions (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-5/test_module.py”, line 39, in test_solutions
self.assertEqual(actual, expected, ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.’)
AssertionError: “Error: Operator must be ‘+’ or ‘-’.” != ’ 32 1 45 123\n- 698 [90 chars] 172’

  • Error: Operator must be ‘+’ or ‘-’.
  • 32 1 45 123
  • 698 - 3801 + 43 + 49

-666 -3800 88 172
: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.


Ran 6 tests in 0.002s

FAILED (failures=2, errors=1)

you need to return an error there, before reaching int('3g5')

this one, and the one after you return an error instead of the arranged numbers

this one returns when it shouldn’t, check if you are using correct symbols

this one does’t execute when you want:
if one of the two is true you get True or False which is true, and then not True making it false and not executing

Thanks . Still having issues with failure. errors- have managed to get rid of. below is the error
 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

None
3 3801 45 123

  • 855 - 2 + 43 + 49

F… 32 1 45 123

  • 698 - 3801 + 43 + 49

-666 -3800 88 172
F…

FAIL: test_arrangement (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-8/test_module.py”, line 10, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]’)
AssertionError: None != ’ 3 3801 45 123\n+ 855 [56 chars]----’ : Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]

======================================================================
FAIL: test_solutions (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-8/test_module.py”, line 39, in test_solutions
self.assertEqual(actual, expected, ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.’)
AssertionError: None != ’ 32 1 45 123\n- 698 [90 chars] 172’ : Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.


Ran 6 tests in 0.001s

FAILED (failures=2)

Code as follows:

def arithmetic_arranger(problems,need_answer=False):
    first_line=''
    second_line=''
    third_line=''
    final_line=''

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

    for prob in problems:
      new_prob=prob.split()
      first,op,second=new_prob[0],new_prob[1],new_prob[2]

      if op not in ['+','-']:
          return "Error: Operator must be '+' or '-'."
      elif len(first)>4 or len(second) >4:
          return 'Error: Numbers cannot be more than four digits.'
      elif not(first.isdigit() and second.isdigit()):
          return 'Error: Numbers must only contain digits.'

      if op=='+':
        answer=int(first)+int(second)

      elif op=='-':
        answer=int(first)-int(second)

      width=max(len(first),len(second))+2
      separation='    '
      first_option=str(first.rjust(width))
      second_option=op+str(second).rjust(width-1)
      third_option=str('-'*width)
      fourth_option =str(answer).rjust(width)

      if len(problems)>0:
          first_line+=first_option+separation
          second_line+=second_option+separation
          third_line+=third_option+separation
          final_line+=fourth_option+separation

      if need_answer==True:
          arranged_problems=first_line+'\n'+second_line+'\n'+third_line+'\n'+final_line
      else:
          arranged_problems=first_line+'\n'+second_line+'\n'+third_line
    return print(arranged_problems)


your functions return nothing, they need to return a string

you can’t return a print statement

but it works in my browser- i am returning a string in my browser.quite lost

this is not returning a string, print does not have a returned value so your function returns None

1 Like

finally worked! thank you for your help @ilenia

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