Hi . I’ve been struggling at what totally seems to be the Operator problem, while being also flagged for arrangements and solutions. Can someone explain where/how to fix it ?
Or what I should study more about? My browser looks so stressed out.
Thank you! I appreciate it!
My code so far
def arithmetic_arranger(problems, show_answer = False):
if len(problems)>5:
return "Error: Too many problems."
else:
fLine = ""
sLine = ""
thLine = ""
ansLine = ""
for i, problem in enumerate(problems):
x=problem.split()
num1 =x[0]
num2 =x[2]
op = x[1]
if op !="+" and op !="-":
return "Error: Operator musr be '+' or '-'."
else:
for digit in num1:
if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
return "Error: Numbers must only contain digits."
else:
continue
for digit in num2:
if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
return "Error: Numbers must only contain digits."
else:
continue
if len(num1)>4 or len(num2)>4:
return "Error: Numbers cannot be more than four digits."
elif op =="+" or op =="-":
width =max(len(num1), len(num2))+2
first = str(num1)
fLine += first.rjust(width)
sLine += str(op +" "+ num2.rjust(width-2))
thLine += str("-"*width)
arr_problems =[]
problem is problem in arr_problems
if i<len(problems)-1:
fLine +=" "
sLine +=" "
thLine +=" "
ansLine +=" "
problem = (fLine+'\n'+sLine+'\n'+thLine)
return " ".join(str(problem) for problem in arr_problems)
if show_answer== True:
if op == "+":
answer1 = int(num1)+int(num2)
ansLine += str(answer1).rjust(width)
else:
answer2 = int(num1)-int(num2)
ansLine += str(answer2).rjust(width)
problem = (fLine+'\n'+sLine+'\n'+thLine+'\n'+ansLine)
return " ".join(str(problem) for problem in arr_problems)
My browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.
There’s no output from your function here; that’s what the '' != bit means in the AssertionError. I double checked it outside the test as well.
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 41, 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: '' != ' 32 1 45 123\n- 698 [89 chars] 172'
+ 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`.
Same here.
This is part of the problem. This is where your function stops. Your code can’t get past this return and never executes the rest, so the answers will never show. Worse, this return is in the first iteration of your outer loop, so it’s not getting through all the problems either.
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.
String formatting in this and the budget app are the hardest parts of these challenges. You may need to add rstrip() to the last line, see my code for an example.
Sorry for adding to the trouble. I should learn to have the proper manners here.
I posted a reply using the (`) just now. I didn’t use (</>) though.
Thank you for all your help!
After investing so many days just to make arr_problems as a list of quatro-deck strings work somehow, I am at peace with rethinking that. I haven’t realized that it was possible to use it as a string which is also a variable that were sliced, until I saw the code built with comments by craig.lunney. Plus, I haven’t known to use the ‘isnumeric()’, either.
So this is totally a Hello Higher World! level of project for me. I must admit that all my suffering was well-deserved and good for me.
I appreciate all the help you have extended to me. You are all truly magnificent!!
Thank you!!!