【发布时间】:2017-08-08 07:54:30
【问题描述】:
所以,每当我调用一个调用另一个函数的函数时,我都会得到这个 TypeError 并且我不知道为什么,因为当我调用第一个函数时它不会发生。代码如下:
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
提前致谢!
【问题讨论】:
-
错误出现在哪里?整个错误消息是什么?
-
刚刚加了,才发现忘记提了
标签: python function python-3.x int typeerror