Python“类型错误:'builtin_function_or_method'对象不可订阅”

时间:2015-10-16 12:00:12

标签: python encryption

我的(最近修复的)代码如下:

cryptograph = {"a":"b", "b":"c", "c":"d", 'd': 'e', 'e':'f', 'f':'g', 'g':'h', 'h':'i', 'i':'j', 'j':'k', 'k':'l', 'l':'m', 'm':'n', 'n':'o', 'o':'p', 'p':'q', 'q':'r', 'r':'s', 's':'t', 't':'u', 'u':'v','v':'w', 'w':'x', 'x':'y', 'y':'z', 'z':'a', ' ': ' ', '.':',', ',':'.', '"':"'", "'":'"', '<':'>', '>':'<', '0':'1', '9':'2', '8':'3', '7':'4', '6':'5', '5':'6', '4':'7', '3':'8', '2':'9', '1':'0'}

def encrypt (string):
    string = string.lower()
    length = len(string)
    toBeTranslated = splitter(string)
    translated = ''

    for letter in toBeTranslated:
        translated = translated + cryptograph[letter]
    print(translated)

def decrypt (string):
    string = string.lower()
    length = len(string)
    toBeTranslated = splitter(string)    
    translated = ''

    for letter in toBeTranslated:
        letter = 
        translated = translated + list(cryptograph.keys())[list(cryptograph.values()).index[letter]]



def splitter (string):
    rotation = 0
    stringLength = len(string)
    charList = []
    for _ in range(stringLength):
        charList.append(string[rotation])
        rotation = rotation + 1
    return charList

我正在尝试创建一个简单的加密应用程序,并遇到了一个问题:

Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    decrypt('hi')
  File "C:\Users\WILLARD\Desktop\encryption.py", line 20, in decrypt
    letter = list(cryptograph.keys())[list(cryptograph.values()).index[letter]]
TypeError: 'builtin_function_or_method' object is not subscriptable

我用谷歌搜索无济于事。

我认为问题可能在于我尝试使用值找到一个dict键:

list(cryptograph.keys())[list(cryptograph.values()).index[letter]]

有趣的是,我使用解释器测试了该行,并且工作正常。

1 个答案:

答案 0 :(得分:1)

  

列表(cryptograph.keys())[表(cryptograph.values())。索引[信]]

尝试改为:

list(cryptograph.keys())[list(cryptograph.values()).index(letter)]
相关问题