Tell us what’s happening:
Why does it say that the formatter output and solution output is incorrect even though the displayed output is clearly correct? It says “Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]” for the first error and “Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.” for the second error, even though the output shows what it’s supposed to look like.
Your code so far
def arithmetic_arranger(problems, solution=False):
line1 = ""
line2 = ""
line3 = ""
line4 = ""
times = len(problems)
counter = 0
arranged_problems = ""
if len(problems) > 5:
return "Error: Too many problems."
for x in problems:
calcs = x.split()
first = calcs
operator = calcs
second = calcs
if operator not in ("+","-"):
return "Error: Operator must be '+' or '-'."
elif not first.isdigit() or not second.isdigit():
return "Error: Numbers must only contain digits."
elif len(first) > 4 or len(second) > 4:
return "Error: Numbers cannot be more than four digits."
if operator == '+':
answer = int(first) + int(second)
if operator == '-':
answer = int(first) - int(second)
dashes = len(max(first,second))+2
line1 = line1 + str(first.rjust(dashes))
line2 = line2 + str(operator + second.rjust(dashes-1))
line3 = line3 + str("-"*dashes)
line4 = line4 + str(answer.rjust(dashes))
counter = counter + 1
if counter != times:
line1 = line1 + " "
line2 = line2 + " "
line3 = line3 + " "
line4 = line4 + " "
if solution == True:
arranged_problems = line1 + "\n" +line2+ "\n"+line3 + "\n" + line4
if solution == False:
arranged_problems = line1 +"\n"+line2+"\n"+line3
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.
I’m aware that it returns ’ ', but do you know what is causing that error? I’ve referenced the arranged_problems variable in the output of the solution but for some reason it is still giving me the ’ ’ Assertion Error.
Thanks JeremyLT and ieahleen, removing the else statement solved the Assertion Error problem. I fixed and optimized some more of the code, but there’s still one odd problem for this challenge.
For some reason the last calculation in the statements prints one less character than expected? From the code, the last calculation’s maximum length is 3, so therefore the number of dashes would be 5. However, the actual statement prints out 4 dashes instead of 5. I’ve tried whitespace stripping the inputs and adding a space when the index of the problem = 4, but it doesn’t work. Are you still able to help with this? Thanks a lot in advance
Firstly thanks to the two forum staff I managed to solve the Assertion Error != (expected output) by eliminating the “else: continue statement”.
Next, for some reason, the the final calculation was 1 character short, so when the maximum length was 3, I expected that there would be 5 dashes. However, there were only 4. So after a while of testing, I redid the dashes statement from
dashes = len(max(first,second))+2
dashes = max(len(first), len(second)) + 2
For some reason, the second statement (which fixed the error) didn’t work in the Python IDLE because int does not have a len() attribute so it wasn’t clear why the code on replit was not working for a long time until I experimented more.