Python – TypeError:’list’对象不可调用

Python – TypeError:’list’对象不可调用

我正在尝试编写一个文本文件,其中包含重新排列的字母表列表,以便每个字母都以不同的字符开头。第一个字母移到末尾,重复。

    alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

    # generate 26 alpabets without typing all
    def alpha_gen(alphabet,a_list):
       x = 0
       while x < 26:
           alphabet += [alphabet.pop(0)]
           key = alphabet(0)
           written = str(key) + ' : ' + str(alphabet) + '\n'
           a_list.write(written)
           x += 1

    def main():
       a_list = open('alpabet_list.txt', 'w')
       alpha_gen(alphabet, a_list)
       a_list.close()

    if __name__ == '__main__': main()

但是我收到此错误:

    File "vigenere_cipher.py", line 13, in alpha_gen
    key = alphabet(0)
    TypeError: 'list' object is not callable

答案1

File "vigenere_cipher.py", line 13, in alpha_gen
key = alphabet(0)
TypeError: 'list' object is not callable

在 Python 中,()表明您想要调用(执行)某个函数。您在第 13 行所做的alphabet(0)是尝试调用alphabet一个列表,而不是一个函数。将其更改为alphabet[0]访问alphabet列表的第一个元素。

相关内容