A small quiz question

Steve D'Aprano steve+python at pearwood.info
Wed Aug 16 07:44:44 EDT 2017


On Wed, 16 Aug 2017 09:06 pm, Stefan Ram wrote:

>   I wrote my first Python quiz question!
> 
>   It goes like this:
> 
>   Can you predict (without trying it out) what the Python
>   console will output after the following three lines have
>   been entered?
> 
> def f(i): print(i); return i;
> 
> f(4)**f(1)**f(2)

My initial prediction was:


4
1
2
16


which is embarrassing. Fortunately my supper arrived in the nick of time 
to distract me from hitting Send, just long enough to remember that 1*1
is 1, not 2.

So how about:

4
1
2
4

Unless its a syntax error... I can't remember if the REPL allows multiple
semi-colon separated statements after a colon declaration. I know it gets mad
at this:

py> def a(): pass; def b(): pass
  File "<stdin>", line 1
    def a(): pass; def b(): pass
                     ^
SyntaxError: invalid syntax


So my wild guess is that this is a trick question and the actual answer is that
its a SyntaxError.

Otherwise, I'm sticking with

4
1
2
4



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list