Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion python2/koans/about_scoring_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@
# Your goal is to write the score method.

def score(dice):
final_score = 0
repeated_nums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for number in dice:
repeated_nums[number]=repeated_nums[number] + 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dice have 6 possible values, but the repeated_nums variable in your solution has 10 total values. Was this done because you were getting list index out of range errors when the array had only 6 values, and increasing the size fixed it? If that is the case, then let me explain why.

Sequences (list, tuple, set, string, etc.) in Python and most programming languages are zero-based. This means that the elements are numbered starting at 0. The first element has index 0, the second element has index 1, and so on. When you create repeated_nums with 6 elements, you get the following:

# Index           0  1  2  3  4  5
# Die Value       1  2  3  4  5  6 
# repeated_nums  [0, 0, 0, 0, 0, 0]

The out of index errors were happening because you were iterating over the values of the dice,

for number in dice:

which set number to some value between 1 and 6, but were using this value as an index.

    repeated_nums[number] [...]

The problem here is that values go from 1 to 6, but indexes go from 0 to 5. This means that whenever the value of number is 6, you will get an list index out of range error, because the list does not have an index 6; its maximum index is 5.

A quick solution to this is to substract 1 from the value of the dice, so that both the indexes and dice values are in the same range.

repeated_nums = [0, 0, 0, 0, 0, 0]
for number in dice:
    repeated_nums[number-1] = repeated_nums[number] + 1

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even notice this error because for some reason I forgot that the dice goes only to 6 (I did not forget that it can't be 0 tho haha)

So what I was trying to do is to compare the values to the numbers from 1 to 9, when trying to make the list shorter I run with that error, so that fixes it!

for i in range(0,10):
aux1 = int (repeated_nums[i] / 3)
aux2 = repeated_nums[i] % 3
if i == 1:
final_score = final_score + (aux1 * 1000) + (aux2 * 100)
elif i == 5:
final_score = final_score + (aux1 * 100 * 5) + (aux2 * 50)
else:
final_score = final_score + (aux1 * 100 * i)
# You need to write this method
pass
return final_score


class AboutScoringProject(Koan):
Expand Down