为什么会出现此错误?
追踪(最近一次通话): 文件" python",第6行,in IndexError:字符串索引超出范围
关于此代码
Cipher = input("Enter encrypted text:")
length = len(Cipher)
start = 0
decrypt=""
while length!=Cipher:
asc=ord(Cipher[int(start)]) #String index out of range
if asc <=95:
decrypt=decrypt+Cipher[start]
elif asc >95 and asc <=110:
num = asc
num=num+13
decrypt=decrypt+chr(num)
elif asc>110 and asc<=122:
num = asc
num=num-13
decrypt=decrypt+chr(num)
else:
decrypt=decrypt+""
start=start+asc
print("DONE!")
我该如何解决?
答案 0 :(得分:0)
Cipher是一个字符串,因此永远不会等于int,即int。 int(length),因为长度已经是一个int ...
Cipher = input("Enter encrypted text:")
length = len(Cipher)
start = 0
decrypt=""
while start!=length: #check for termination condition
asc=ord(Cipher[start])
if asc <=95:
decrypt=decrypt+Cipher[start]
elif asc >95 and asc <=110:
num = asc
num = num+13
decrypt=decrypt+chr(num)
elif asc>110 and asc<=122:
num = asc
num = num-13
decrypt=decrypt+chr(num)
else:
decrypt=decrypt+""
start=start+1 # increment counter
答案 1 :(得分:0)
Cipher = list(input("Enter encrypted text:"))
length = len(Cipher)
start = 0
decrypt=""
while start != length:
asc=ord(Cipher[start])
if asc <=95:
decrypt=decrypt+Cipher[start]
elif asc >95 and asc <=110:
num = asc
num=num+13
decrypt=decrypt+chr(num)
elif asc>110 and asc<=122:
num = asc
num=num-13
decrypt=decrypt+chr(num)
start=start+1
print("DONE! Result: " + decrypt)
您可以在此处试用:https://repl.it/Nn05/0