Python Forum
Subtracting gives a keyerror:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subtracting gives a keyerror:
#1
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()
Reply
#2
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.
Reply
#3
(Oct-03-2016, 02:48 AM)Larz60+ Wrote: 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.

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.
Reply
#4
'KeyError' suggests that the key you passed to a dictionary is not present in that dictionary.
Reply
#5
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.
Reply
#6
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
Reply
#7
(Oct-03-2016, 11:02 AM)Larz60+ Wrote: 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
Did you call the function a good 1/2 dozen times?
Reply
#8
@[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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with subtracting values using SQLite & Python Extra 10 3,288 May-10-2022, 08:36 AM
Last Post: ibreeden
  Subtracting datetimes [index] Mark17 2 2,409 Aug-21-2021, 12:11 AM
Last Post: Larz60+
  Indexing problem while iterating list and subtracting lbtdne 2 2,087 May-14-2020, 10:19 PM
Last Post: deanhystad
  Subtracting values between two dictionaries/ floating point numbers FloppyPoppy 5 5,828 Mar-04-2019, 01:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020