on writing a while loop for rolling two dice

Hope Rouselle hrouselle at jevedi.com
Sat Aug 28 17:17:26 EDT 2021


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> Hope Rouselle <hrouselle at jevedi.com> writes:
>>Wait, I'm surprised ``outcome'' is still a valid name at the
>>return-statement.  Wasn't it defined inside the while?  Shouldn't its
>>scope be restricted to the while block?  I had no idea.  I should learn
>>some Python.
>
>   In Python, local names can be introduced by an assignment
>   and have function scope. There is no block scope in Python.
>
>   Below, "name" /is/ a local name already, but is being 
>   used before being assigned to.
>
> def function():
>     if False:
>         name = 0
>     return name
>
> function()
>
> # return name
> # UnboundLocalError: local variable 'name' referenced before assignment

I appreciated the example.  I had no idea.  (I had looked up the rules
and it was pretty simple to understand, but an example is always nice.)
Thank you so much.


More information about the Python-list mailing list