on writing a while loop for rolling two dice
Hope Rouselle
hrouselle at jevedi.com
Sat Aug 28 16:50:01 EDT 2021
Hope Rouselle <hrouselle at jevedi.com> writes:
> ram at zedat.fu-berlin.de (Stefan Ram) writes:
>
>> Hope Rouselle <hrouselle at jevedi.com> writes:
>>>How would you write this?
>>
>> """Rolls two dice until both yield the same value.
>> Returns the number of times the two dice were rolled
>> and the final value yielded."""
>> roll_count = 0
>> while True:
>> outcome = roll_two_dice()
>> roll_count += 1
>> if outcome[ 0 ]== outcome[ 1 ]: break
>> return roll_count, outcome[ 0 ]
>
> You totally convinced me. Thanks.
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.
More information about the Python-list
mailing list