Subtracting gives a keyerror: - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Subtracting gives a keyerror: (/thread-251.html) |
Subtracting gives a keyerror: - xepicxmonkeyx - Oct-02-2016 To give some conext to the code you'll find in the link below, I'm making a system which brute forces the game Peg Solataire. It's grid is laid out in the dictionary, and within that dictionary is a series of key's incrementing in base-seven numerals which are given a dictionary of there own containing their X and Y position in the 7x7 grid, as well as the symbol they are to be portrayed with. The '@' is an invalid position used basically for filler text in order to get that cross-shape. The 'O's are to symbolize any position that is currently both a valid position and occupied by a peg. The 'P's to symbolize any valid position not currently occupied. In order to have a peg jump over another into an unoccupied position, I choose at random a position then check if the position with +2 to its X axis is occupied, if not, is there a peg +1 to its X axis to jump over. If so make current position 'P', the +1 position 'P'. And the +2 position that was 'P', now an 'O'. My code instead adds +2 to what should be the 'Y' axis, and comes up with an error when I try to subtract from it (or if I add to it a negative number). Not only that, but when I add or subtract from what should be the 'Y' axis, it comes up with the same error. Of course with the randomized co-ordinates, the KeyError: 'Value' changes. And if I only subtract once or twice, the error doesn't appear, but I need to be able to call it as many times as needed. I've only called 'switch_target()' once so you can see what outputs, but (as I said) the error occurs only after multiple callings, so increment the number of times according to your bandwidth. The link to it is: link removed, code added below from __future__ import print_function import random list = { 11: {'x': 1, 'y': 1, 'onScreen': '@'}, 12: {'x': 1, 'y': 2, 'onScreen': '@'}, 13: {'x': 1, 'y': 3, 'onScreen': 'O'}, 14: {'x': 1, 'y': 4, 'onScreen': 'O'}, 15: {'x': 1, 'y': 5, 'onScreen': 'O'}, 16: {'x': 1, 'y': 6, 'onScreen': '@'}, 17: {'x': 1, 'y': 7, 'onScreen': '@'}, 21: {'x': 2, 'y': 1, 'onScreen': '@'}, 22: {'x': 2, 'y': 2, 'onScreen': '@'}, 23: {'x': 2, 'y': 3, 'onScreen': 'O'}, 24: {'x': 2, 'y': 4, 'onScreen': 'O'}, 25: {'x': 2, 'y': 5, 'onScreen': 'O'}, 26: {'x': 2, 'y': 6, 'onScreen': '@'}, 27: {'x': 2, 'y': 7, 'onScreen': '@'}, 31: {'x': 3, 'y': 1, 'onScreen': 'O'}, 32: {'x': 3, 'y': 2, 'onScreen': 'O'}, 33: {'x': 3, 'y': 3, 'onScreen': 'O'}, 34: {'x': 3, 'y': 4, 'onScreen': 'O'}, 35: {'x': 3, 'y': 5, 'onScreen': 'O'}, 36: {'x': 3, 'y': 6, 'onScreen': 'O'}, 37: {'x': 3, 'y': 7, 'onScreen': 'O'}, 41: {'x': 4, 'y': 1, 'onScreen': 'O'}, 42: {'x': 4, 'y': 2, 'onScreen': 'O'}, 43: {'x': 4, 'y': 3, 'onScreen': 'O'}, 44: {'x': 4, 'y': 4, 'onScreen': 'P'}, 45: {'x': 4, 'y': 5, 'onScreen': 'O'}, 46: {'x': 4, 'y': 6, 'onScreen': 'O'}, 47: {'x': 4, 'y': 7, 'onScreen': 'O'}, 51: {'x': 5, 'y': 1, 'onScreen': 'O'}, 52: {'x': 5, 'y': 2, 'onScreen': 'O'}, 53: {'x': 5, 'y': 3, 'onScreen': 'O'}, 54: {'x': 5, 'y': 4, 'onScreen': 'O'}, 55: {'x': 5, 'y': 5, 'onScreen': 'O'}, 56: {'x': 5, 'y': 6, 'onScreen': 'O'}, 57: {'x': 5, 'y': 7, 'onScreen': 'O'}, 61: {'x': 6, 'y': 1, 'onScreen': '@'}, 62: {'x': 6, 'y': 2, 'onScreen': '@'}, 63: {'x': 6, 'y': 3, 'onScreen': 'O'}, 64: {'x': 6, 'y': 4, 'onScreen': 'O'}, 65: {'x': 6, 'y': 5, 'onScreen': 'O'}, 66: {'x': 6, 'y': 6, 'onScreen': '@'}, 67: {'x': 6, 'y': 7, 'onScreen': '@'}, 71: {'x': 7, 'y': 1, 'onScreen': '@'}, 72: {'x': 7, 'y': 2, 'onScreen': '@'}, 73: {'x': 7, 'y': 3, 'onScreen': 'O'}, 74: {'x': 7, 'y': 4, 'onScreen': 'O'}, 75: {'x': 7, 'y': 5, 'onScreen': 'O'}, 76: {'x': 7, 'y': 6, 'onScreen': '@'}, 77: {'x': 7, 'y': 7, 'onScreen': '@'} } # produces the Peg Grid, and outputs it grid_list = [] def make_grid(): count = [] new_line = 0 for k, v in list.items(): new_line += 1; count.append(v['onScreen']) if new_line % 7 == 0: count.append('\n') return count # converting from base-ten numbers to base-seven numbers for grid co-ords def switch_target(): print(*make_grid(), sep="") selection = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48] selectionCoords = [11, 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, 27, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 47, 51, 52, 53, 54, 55, 56, 57, 61, 62, 63, 64, 65, 66, 67, 71, 72, 73, 74, 75, 76, 77] choice = random.choice(selection) coords = selectionCoords[choice] print(choice) print(coords) if list[coords]['x'] <= 5: twoOverKey = str(int(list[coords]['x']) + 2) + str(list[coords]['y']); else: twoOverKey = str(1) + str(1); twoOver = list[int(twoOverKey)]['onScreen'] if list[int(coords)]['onScreen'] == '@' or list[int(coords)]['onScreen'] == 'P': pass elif list[int(coords)]['onScreen'] == 'O' and twoOver == 'P' and list[int(str(list[coords]['x'] + 1) + str(list[coords]['y']))]['onScreen'] == 'O': list[int(coords)]['onScreen'] = 'P' list[int(str(list[coords]['x'] + 1) + str(list[coords]['y']))]['onScreen'] = 'P' list[int(twoOverKey)]['onScreen'] = 'O'; else: pass print(twoOverKey + ' l') switch_target() RE: Subtracting gives a keyerror: - Larz60+ - Oct-03-2016 Please post your actual error traceback it is always very important in addition, just before the line that the code is failing on, add print statements for all variables that are used in the failing line. RE: Subtracting gives a keyerror: - xepicxmonkeyx - Oct-03-2016 (Oct-03-2016, 02:48 AM)Larz60+ Wrote: Please post your actual error traceback it is always very important The error is: Traceback (most recent call last) File "python", line 108, in <module> File "python", line 92, in switch_target KeyError: 'Number'And the print out has been added. RE: Subtracting gives a keyerror: - j.crater - Oct-03-2016 'KeyError' suggests that the key you passed to a dictionary is not present in that dictionary. RE: Subtracting gives a keyerror: - Crimson King - Oct-03-2016 One thing, Try not to use language keywords, like list, for your own data structures. That's going to mess things up for you in the future. I'll try to have a deeper look at the code in a few minutes. RE: Subtracting gives a keyerror: - Larz60+ - Oct-03-2016 Hello, When I run it on my system, I don't get an error: results: @@OOO@@ @@OOO@@ OOOOOOO OOOPOOO OOOOOOO @@OOO@@ @@OOO@@ 6 17 37 l Process finished with exit code 0 RE: Subtracting gives a keyerror: - xepicxmonkeyx - Oct-03-2016 (Oct-03-2016, 11:02 AM)Larz60+ Wrote: Hello,Did you call the function a good 1/2 dozen times? RE: Subtracting gives a keyerror: - micseydel - Oct-03-2016 @[xepicxmonkeyx], rather than expecting people to run the code until it breaks, remove your random import and hard-code a value that reproduces your problem. That way everyone will always be talking about precisely the same problem. |