2017-03-16 121 views
-1

所以,每当我打电话调用另一个函数,我得到这个类型错误的功能,我不知道是什么原因,因为这时候我先打电话功能不会发生。下面的代码:收到类型错误:“诠释”对象不是标化(嵌套函数)

def codeChar(c,key): 
    k = ord(c) + key 
    if key > 26: 
     key = key % 26 
    if 91 <= k <= 96: 
     k = k - 26 
    elif 123 <= k: 
     k = k - 26 
    c = chr(k) 
    return c 

def codeBlock(word,key): 
    i = 0 
    result = "" 
    while i < len(word): 
     k = int(key[i]) 
     result = result + codeChar(word[i],k) 
     i = i + 1 
    return result 

def isletter(h): 
    i = ord(h) 
    if 65 <= i <= 90: 
     return True 
    elif 97 <= i <= 122: 
     return True 
    else: 
     return False 

def codeString(string,key): 
    i = 0 
    result = "" 
    while i < len(string): 
     k = int(key[i]) 
     if isletter(string[i]) == True: 
      result = result + codeBlock(string[i],k) 
      i = i + 1 
     else: 
      i = i + 1 
    return result 

print(codeString(input("Enter a sentence to be coded: "),input("Enter an 8 digit key: "))) 

错误代码,当我运行它是这样的:

Enter a sentence to be coded: Hello world 
Enter your student number: 16061226 
Traceback (most recent call last): 
    File "E:\cw.1\cw.1.py", line 89, in <module> 
    print(codeString(input("Enter a sentence to be coded: "),input("Enter your student number: "))) 
    File "E:\cw.1\cw.1.py", line 82, in codeString 
    result = result + codeBlock(string[i],k) 
    File "E:\cw.1\cw.1.py", line 39, in codeBlock 
    k = key[i] 
TypeError: 'int' object is not subscriptable 

提前感谢!

+1

哪里发生错误?什么是整个错误信息? –

+0

刚添加,实现了我忘了提到它 – Jack

回答

0

当您通过第36行的kcodeBlock时,它是一个整数,而不是函数期望的字符串。也许你打算在这里使用key

+0

当我使用键I接收IndexError:字符串索引超出范围为'结果=结果+码块(串[I],键[I])'线 – Jack

+0

这是由于你认为字符串比关键字短。如果字符串比键长,那么你会得到你描述的错误。 –

0

它无关调用从另一个函数的函数。当你从codeString内调用codeBlock,传递给它的参数k,它是一个整数。在另一边,随着codeBlock功能key,试图索引该整数做int(key[i]),因此,类型错误。

相关问题