Python 3.x 索引器错误:字符串索引超出范围。编码新手
我是Python的初学者,所以我得到了错误 回溯(最近一次呼叫最后一次): 文件“C:/Users/spchee/Documents/cc.py”,第43行,在 c(“helloz”,2)文件“c:/Users/spchee/Documents/cc.py”,第35行,在c中 添加到字符串(n)文件“C:/Users/spchee/Documents/cc.py”,第5行,添加到字符串中 如果消息[i].titlecase(): 索引器错误:字符串索引超出范围 我目前的代码是:Python 3.x 索引器错误:字符串索引超出范围。编码新手,python-3.x,caesar-cipher,stringindexoutofbounds,Python 3.x,Caesar Cipher,Stringindexoutofbounds,我是Python的初学者,所以我得到了错误 回溯(最近一次呼叫最后一次): 文件“C:/Users/spchee/Documents/cc.py”,第43行,在 c(“helloz”,2)文件“c:/Users/spchee/Documents/cc.py”,第35行,在c中 添加到字符串(n)文件“C:/Users/spchee/Documents/cc.py”,第5行,添加到字符串中 如果消息[i].titlecase(): 索引器错误:字符串索引超出范围 我目前的代码是: import m
import math
message = ""
i=0
def add_to_string(modifier):
if message[i].titlecase():
encrypted = encrypted + string[modifier].upper()
else:
encrypted = encrypted +string[modifier]
def c(message,key):
string = "0abcdefghijklmnopqrstuvwxyz0"
encrypted = ""
legnth_message = len(message)
i = 0
for i in range (legnth_message-1):
n = 0
x = 0
while message[i] != string[x].lower():
x+=1;
while string[int(x)+int(n)] != "0":
n += math.copysign(1, key)
if abs(key)<abs(n):
n = (abs(n)-abs(key))*math.copysign(1,key)
if math.copysign(1,key) == 1:
add_to_string(n)
else:
add_to_string(27+n)
else:
add_to_string(x+key)
c("helloz", 2)
导入数学
message=“”
i=0
def将_添加到_字符串(修饰符):
如果消息[i].titlecase():
encrypted=encrypted+string[modifier].upper()
其他:
加密=加密+字符串[修饰符]
def c(信息,钥匙):
string=“0abcdefghijklmnopqrstuvxyz0”
加密=“”
legnth_message=len(message)
i=0
对于范围内的i(legnth_消息-1):
n=0
x=0
while message[i]!=字符串[x]。下限()
x+=1;
而字符串[int(x)+int(n)]!="0":
n+=数学复制符号(1,键)
如果abs(key)您的消息在这一行中为空`if message[i].titlecase():`那么它将如何访问消息[i]?您的消息在这一行中为空`if message[i].titlecase():`那么它将如何访问消息[i]?