0
def mob_attack(minimum_roll, mob_size):

table = {
        range(1, 5) : 1,
        range(6, 12) : 2,
        range(13, 14) : 3,
        range(15, 16) : 4,
        range(17, 18) : 5
        }

for key in table:
    if minimum_roll in key:
        return int(mob_size/table[key])

The goal for this code is to return the quotient of the mob_size, using the value as the divisor of the key that matches with minimum_roll. The problem I'm experiencing is that it returns None for the max number in the Range keys, i.e 5, 12, 14, 16, and 18. How do I fix this so it returns the value that it should?

  • 1
    Its because the `range()` function returns a sequence of numbers, starting from 0 by default, and increments by 1, and stops before a specified number. So in your case the table dictionary should be like this `table = { range(1, 6) : 1, range(6, 13) : 2, range(13, 15) : 3, range(15, 17) : 4, range(17, 19) : 5 }` – Osama Naveed Sep 19 '21 at 23:44
  • Does it even work? Just asking because https://stackoverflow.com/questions/39358092/range-as-dictionary-key-in-python – tevemadar Sep 19 '21 at 23:51
  • @tevemader. It'll work with Python 3. – Frank Yellin Sep 20 '21 at 00:57
  • 1
    you could use normal tuple `(1, 5)` and check `for start, end in table: if start <= minimum_roll <= end: ...` – furas Sep 20 '21 at 02:41

0 Answers0