what trying store x,y value key. example, in case, need once x,y has reached value of 3, process happens.
here have tried: however, gives me error, unable store lists within dictionaries.
dictionary= {} player = [0,0] def checking(): if player in dictionary: dictionary[[player]] +=1 print("repopulate", dictionary) else: dictionary[player] = 0 print(dictionary) if dictionary.get(player) >= 3: print("it done!")
edit: sorry lack of clarity in question. player variable user input of user wishes move within x,y given. there multiple treasures, , if user chose position x,y same treasure x,y; +1 should added treasure. once treasure reaches 3, should diminished.
i think want use player key, , count value of key:
>>> treasure_hits = {} >>> player = (0,0) >>> try: ... treasure_hits[player] += 1 ... except keyerror: ... treasure_hits[player] = 0 ... >>> treasure_hits {(0, 0): 0} >>> >>> try: ... treasure_hits[player] += 1 ... except keyerror: ... treasure_hits[player] = 0 ... >>> treasure_hits {(0, 0): 1} >>>
making treasure_hits tuple instead of list allows used key since immutable
Comments
Post a Comment