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)
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.
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
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)