简体   繁体   中英

How to handle TypeError exception raised by python dict?

I cannot seem to find the error to this code. I keep getting this as my output log.

 STDERR
Traceback (most recent call last):
  File "main.py", line 18, in <module>
    static ("Avoid success at all costs!")
  File "main.py", line 10, in static
    test.assert_equals(rot13(d),sol(d))
  File "/home/codewarrior/solution.py", line 16, in rot13
    newkey = (get_keyA(i)+13)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

The purpose of this code is to active Rot13, if there is a numerical in there or a space, it'll just leave it as original.

ABC = {1: "A", 2: "B", 3:"C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H", 9: "I", 10: "J", 11: "K",
       12: "L", 13: "M", 14: "N", 15: "O", 16: "P", 17: "Q", 18: "R", 19: "S",
       20: "T", 21: "U", 22: "V", 23: "W", 24: "X", 25: "Y", 26: "Z"}
abc = {1: "a", 2: "b", 3:"c", 4: "d", 5: "e", 6: "f", 7: "g", 8: "h", 9: "i", 10: "j", 11: "k",
       12: "l", 13: "m", 14: "n", 15: "o", 16: "p", 17: "q", 18: "r", 19: "s",
       20: "t", 21: "u", 22: "v", 23: "w", 24: "x", 25: "y", 26: "z"} 

def rot13(message):
    string = list(message)
    new = ""
    for i in string:
        if (i.isnumeric() == False):
            if (i != " "):
                if (i.islower() == False):
                    newkey = (get_keyA(i)+13)
                    if (newkey > 26):
                        newkey -= 26
                    new += ABC[newkey]
                if (i.islower() == True):
                    newkey = (get_keya(i)+13)
                    if (newkey > 26):
                        newkey -= 26
                    new += abc[newkey] 
            else:
                new += i
        else :
            new += i
    return new
    
    
def get_keyA(val): 
    for key, value in ABC.items(): 
        if val == value: 
            return key 

def get_keya(val): 
    for key, value in abc.items(): 
        if val == value: 
            return key 

Yes, I'm not using encode. I want to try this out without using it. Thank you for reading and thanks for the help!

your functions are written wrongly and non pythonic. we will fix later. the bug lays in this line:

newkey = (get_keyA(i)+13)

you have called the method with i that doesnot exist in the ABC dict . and you have get None return value from this method. hence, None + 13 is something that python cant handle (None + int) . hence the error!

Any way this methods are unusable. python have built in methods for searching a dict and get the return value:

dict.get(key[, value]) 

get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

Just use it. i recommend you to add a try except on this code block(above) Suppose that, if the key i is not exist in the dict , then you can return a default value that suits your program, so th e usage become:

ABC.get(i, <some_default_value>) 

for example: suppose i want to return 0 value if the key i isnot exist in the dict, and then i want to add it to the dict with default value 'foo', so the code becomes:

if not ABC.get(i,0):
    ABC[i] = 'foo'  #add the key i to this dict with the value 'foo'

hence the methods you have wrote ( get_keyA(val) and get_keya(val) ), can be deleted!

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