Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引错误:字符串超出范围,但不应为';你不会在射程之外吗?_Python_Python 3.x_Encryption - Fatal编程技术网

Python 索引错误:字符串超出范围,但不应为';你不会在射程之外吗?

Python 索引错误:字符串超出范围,但不应为';你不会在射程之外吗?,python,python-3.x,encryption,Python,Python 3.x,Encryption,好吧,我正在制作一个编码器/解码器,目前我正在测试我的想法是否可行,但我不断收到错误,告诉我我的字符串索引超出范围,而它本来就不应该超出范围 message = "abc" #Should come out as 212223 translated = ' ' n = 1 while n >= 0: t = message[n] if t == 'a': translated = translated + '21' elif t == 'b':

好吧,我正在制作一个编码器/解码器,目前我正在测试我的想法是否可行,但我不断收到错误,告诉我我的字符串索引超出范围,而它本来就不应该超出范围

message = "abc"
#Should come out as 212223
translated = ' '


n = 1
while n >= 0:
    t = message[n]
    if t == 'a':
        translated = translated + '21'
    elif t == 'b':
        translated = translated + '22'
    elif t == 'c':
        translated = translated + '23'
    while n <= len(message):
        n = n + 1
print(translated)
message=“abc”
#应该是212223
已翻译=“”
n=1
当n>=0时:
t=消息[n]
如果t==“a”:
已翻译=已翻译+21
elif t==“b”:
已翻译=已翻译+22
elif t==“c”:
已翻译=已翻译+23
而
当你继续递增
n
时,你有一个无限循环。 在某个时刻,
消息[n]
将超出范围

您应该在n时移动

当你继续递增
n
时,你有一个无限循环。 在某个时刻,
消息[n]
将超出范围

当n时,应将
移动到此处:

而n此处:


而n因为在最后一个循环中您使用的是t=message[3]…这是导致错误的原因。如果消息变量包含“abc”,则只能访问t=消息[0],t=消息[1],t=消息[2]。所以试试这个

message = "abc"
#Should come out as 212223
translated = ' '


n = 1
while n >= 0:
    t = message[n-1]
    if t == 'a':
        translated = translated + '21'
    elif t == 'b':
        translated = translated + '22'
    elif t == 'c':
        translated = translated + '23'
    while n <= len(message):
        n = n + 1
print(translated)
message=“abc”
#应该是212223
已翻译=“”
n=1
当n>=0时:
t=消息[n-1]
如果t==“a”:
已翻译=已翻译+21
elif t==“b”:
已翻译=已翻译+22
elif t==“c”:
已翻译=已翻译+23

而n是因为在最后一个循环中,您使用的是t=message[3]…这是导致错误的原因。如果消息变量包含“abc”,则只能访问t=消息[0],t=消息[1],t=消息[2]。所以试试这个

message = "abc"
#Should come out as 212223
translated = ' '


n = 1
while n >= 0:
    t = message[n-1]
    if t == 'a':
        translated = translated + '21'
    elif t == 'b':
        translated = translated + '22'
    elif t == 'c':
        translated = translated + '23'
    while n <= len(message):
        n = n + 1
print(translated)
message=“abc”
#应该是212223
已翻译=“”
n=1
当n>=0时:
t=消息[n-1]
如果t==“a”:
已翻译=已翻译+21
elif t==“b”:
已翻译=已翻译+22
elif t==“c”:
已翻译=已翻译+23
而
while n <= len(message):
    n = n + 1
while n < len(message):
    n = n + 1
message = "abc"
#Should come out as 212223
translated = ' '


n = 1
while n >= 0:
    t = message[n-1]
    if t == 'a':
        translated = translated + '21'
    elif t == 'b':
        translated = translated + '22'
    elif t == 'c':
        translated = translated + '23'
    while n <= len(message):
        n = n + 1
print(translated)