我正在 python 中制作一个简单的替换密码。我希望这个程序遍历字符串中的字符,然后将它们的数值添加到数组中,但它给了我这个错误:
Traceback (most recent call last):
File "D:\py programs\simple cypher.py", line 39, in <module>
x[i]=18
IndexError: list assignment index out of range
这是我的代码:
pt=raw_input('string to encrypt ')
x=[]
for i in range (0, len(pt)):
if pt[i]=='a':
x[i]=1
elif pt[i]=='b':
x[i]=2
elif pt[i]=='c':
x[i]=3
elif pt[i]=='d':
x[i]=4
elif pt[i]=='e':
x[i]=5
elif pt[i]=='f':
x[i]=6
elif pt[i]=='g':
x[i]=7
elif pt[i]=='h':
x[i]=8
elif pt[i]=='i':
x[i]=9
elif pt[i]=='j':
x[i]=10
elif pt[i]=='k':
x[i]=11
elif pt[i]=='l':
x[i]=12
elif pt[i]=='m':
x[i]=13
elif pt[i]=='n':
x[i]=14
elif pt[i]=='o':
x[i]=15
elif pt[i]=='p':
x[i]=16
elif pt[i]=='q':
x[i]=17
elif pt[i]=='r':
x[i]=18
elif pt[i]=='s':
x[i]=19
elif pt[i]=='t':
x[i]=20
elif pt[i]=='u':
x[i]=21
elif pt[i]=='v':
x[i]=22
elif pt[i]=='w':
x[i]=23
elif pt[i]=='x':
x[i]=24
elif pt[i]=='y':
x[i]=25
elif pt[i]=='z':
x[i]=26
elif pt[i]==' ':
x[i]='_'
print x
有人可以帮忙吗?