1.0 KiB
Dice Lab Assessment
Everything works as expected. Nice work!
Comments
I love your decision to comment your code, and to write the generalized
function findn
. Within that function, your loop iterating over die
faces works fine, but checking whether nmuch[die]
is initialized
will become tiresome after a while. There's a nice idiom for this,
using defaultdict. A defaultdict is like a normal dict, but when you
first access a key, its value is initialized for you if it's not
already present.
from collections import defaultdict
def findn(self, nofakind):
nmuch = defaultdict(int)
for die in dice.faces():
nmuch[die] += 1
if nmuch[die] == nofakind:
return True
return False
Just a little cleaner :)
Within your yachtzee_goals
, I'm guessing that you probably had a feeling
that copying the same method over into multiple classes wasn't the best way
to do it--this would be a perfect place to use a subclass or a mixin,
python's approach to inheritance and multiple inheritance respectively.