Console says it's incorrect even though the output shows that it is correct

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[0]
    operator = calcs[1]
    second = calcs[2]

    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."
    else:
      continue
    
    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 + "    "
    else:
      continue

    if solution == True:
      arranged_problems = line1 + "\n" +line2+ "\n"+line3 + "\n" + line4
    if solution == False:
      arranged_problems = line1 +"\n"+line2+"\n"+line3

  return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Arithmetic Formatter

Link to the challenge:


https://repl.it/@lokilokhy/boilerplate-arithmetic-formatter#arithmetic_arranger.py

in both failed tests your function returns ""

you may want to look again at your code

What do you mean by that?

circled in red the output of your function as the tests receive. In these two cases your function returns an empty string

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.

I’m not sure if it is related to the core issue, but I would remove those continue statements. They are not intended to be used like that. They should only be used to skip a loop iteration.

If you don’t want anything done in the else clause, don’t include an else clause.

all your code is inside the loop, after a return or continue statement, meaning it’s never executed
I have added some print statements to your code to see what execute and what doesn’t
https://repl.it/@Ieahleen/boilerplate-arithmetic-formatter

1 Like

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

I solved the error!

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

to:

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.