简体   繁体   中英

KeyError python

I wrote a program that shifts letters in a string and returns a dictionary. It works, unless the letter 'w' is within the string. Does anyone know why this happens? This is my code:

def buildCoder(shift):
    import string
    impStr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    resultDict = {}

    lowerValues = dict()
    for index, letter in enumerate(string.ascii_lowercase):
        lowerValues[letter] = index + 1

    upperValues = dict()
    for index, letter in enumerate(string.ascii_uppercase):
        upperValues[letter] = index + 1

    inv_lowerValues = {v:k for k, v in lowerValues.items()}

    inv_upperValues = {v:k for k, v in upperValues.items()}

    for char in impStr:
        if char in string.ascii_lowercase:
            value = lowerValues[char]
            value += shift
            if value <= 25:
                resultDict[char] = inv_lowerValues[value]
            else:
                value = abs(26-value)
                resultDict[char] = inv_lowerValues[value]
        elif char in string.ascii_uppercase:
            value = upperValues[char]
            value += shift
            if value <= 25:
                resultDict[char] = inv_upperValues[value]
            else:
                value = abs(26-value)
                resultDict[char] = inv_upperValues[value]
    return resultDict

This is the error I get (if 'w' is included in impStr ):

KeyError                                  Traceback (most recent call last)
<ipython-input-117-0f4ac72e3c63> in <module>()
----> 1 buildCoder(3)

/var/folders/CV/CVkEwo3TGuu4+2cStWCRyE+++TQ/-Tmp-/tmpMr_sV1.py in buildCoder(shift)

KeyError: 0 

I also saw this error:

Traceback (most recent call last):
  File "submission.py", line 24, in buildCoder
    resultDict[char] = inv_lowerValues[value]
KeyError: 0

*** ERROR: Expected to find a dictionary in the line:
Traceback (most recent call last):
  File "submission.py", line 24, in buildCoder
    resultDict[char] = inv_lowerValues[value]
KeyError: 0

 ***

Let's use a bit of introspection on your code:

lowerValues['w'] == 23
inv_lowerValues[23] == 'w'

# presumably shift == 3

for char in impStr: # when char==w
    if char in string.ascii_lowercase: # it is
        value += shift # 23 + 3 == 26
        if values <= 25 # it's not
        else:
            value = abs(26-value)
            # abs( 26 - 26 ) == 0
            resultDict[char] = inv_lowerValues[value]
            # resultDict['w'] = inv_lowerValues[0]; KeyError

It sounds like you just want to be sure that your shift wraps around the alphabet. That's not abs(26-value) , that's (value % 25)+1 .

I think, you want to write

value = abs(value-25)

instead of

value = abs(26-value)

otherwise you would get 0 as value.

A more concise version of what you are doing:

import string

def buildCoder(shift):
    alpha = string.ascii_lowercase
    ALPHA = string.ascii_uppercase
    if 0 <= shift < 26:
        unshifted_letters = ALPHA + alpha
        shifted_letters = ALPHA[shift:] + ALPHA[:shift] + alpha[shift:] + alpha[:shift]
        return dict(zip(unshifted_letters, shifted_letters))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM