Skip to main content
2 of 2
added 227 characters in body
Roland Illig
  • 21.9k
  • 2
  • 36
  • 83

(Update: as stated in the comment, the codewars task explicitly demands that an empty expression is to return 0. This makes my following remarks invalid for this particular task. In all other situations they still apply.)

There's one thing that you should do in the last lines of your code:

    if stack == []:
        return 0
    else:
        return float(stack[0])

The task explicitly says that you may assume the input is error-free and that your program may do whatever it wants on erroneous input. You currently ignore the error and silently return a wrong result (0). You should not do that.

Instead you should just write:

    return stack[0]

The stack is assumed to only contain floats. Therefore it is not necessary to convert the float to a float again. If you want to check it explicitly:

    assert stack[0] == float(stack[0])
    return stack[0]

Trying to return the result from an empty stack should result in an exception. That way you know for sure that there was something wrong. That's much better than continuing the calculation with a wrong result.

Roland Illig
  • 21.9k
  • 2
  • 36
  • 83