I am trying to create a program that returns a position on a board (starting from position 0) for each roll of a dice.
My dictionary contains keys, which are the positions on the board and values, which are the
displacements that should take place.
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
For example, when I land on position 64, I find key 64 in the dictionary and from 64 I move -4 on the board, resting on 60.
I have my current code below.
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
if position not in dictionary:
pass
if position in dictionary:
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
I am getting partial positions and I believe some iterations are returning a type error.
>>> location([1, 4, 5])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 12, in location
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
38
42
# desired output
[38, 42, 26]
From the first roll of the dice, we land on position 1. We located 1 in my dictionary and learned that I
have to move by 37, resting on 38. From the second roll of the dice, we move 4 from 38 and land
on 42. 42 is not in my dictionary, so we rest on 42. Then from the third roll of the dice, we move 5
from 42, landing on 47. 47 is in my dictionary and I move -21 from 47, landing on 26.
So I am actually getting the first two positions in the list. I have not a single clue why the third
position is not being printed and returning this type error.
locationwas intended to do5isn't in the dict sodictionary.get(roll)returnsNone, which can't be added to an integer