generated from mwc/lab_dice
Add assessment
This commit is contained in:
parent
a3bc21bee7
commit
4fe9b95eda
|
@ -0,0 +1,32 @@
|
||||||
|
# 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.
|
Loading…
Reference in New Issue