Brilliant mystery function

What is the return value of mystery(4) ?

# python code start
def mystery(n):
    answer = 0
    while n > 0:
        answer = answer + 1
        n = n - 1
    answer = answer - 2
    return answer


The answer is 2.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

The while loop executes 4 times, for values n = [4, 3, 2, 1]. answer increases by 1 each time, so by the end of the loop, answer == 4 . Finally, subtract 2 from answer to give the final value: 2 .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...