找不到子字符串错误

时间:2016-06-23 12:17:05

标签: python string substring

我必须用alphapet中的两个字符替换每个字符。例如giac。在制作程序解决问题时,我总是遇到这个错误

substring not found

这是我的代码:

string="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
#=========this is the string on the site
alphapet="abcdefghijklmnopqrstuvwxyz"
#this is the alphapet ofcourse
x=""
y=0
z=""
m=0
#that was declaring variables
#the following is a loop which would go through string
for i in string:
    #the if statement should check if i was in alphapet string
    if i in alphapet:
        if i=="y":
            string.replace(i,"a")
        elif i=="z":
            string.replace(i,"b")
         #the previous two conditions were special cases
        else:
            #the x will equal the first character as m =0 which is"g"
            x=string[m]
            #the y should equal the index of the character in alphapet this is were the error comes
            y=alphapet.index(x)
            #the z should equal the letter that comes after it by 2 in alphapet
            z=alphapet[y+2]
            #the following line should replace it
            string.replace(i,z)
            m+=1
print(string)

4 个答案:

答案 0 :(得分:1)

运行时

x=string[m]

x将等于m中的索引string,包括标点符号和空格等。

所以,在运行时

y=alphapet.index(x)

该程序会引发错误,因为alphapet中没有标点符号或空格。

您需要做的是修改代码以处理标点符号,或者将标点符号添加到alphapet

答案 1 :(得分:1)

s = "g fmnc wms bgblr rpylqjyrc gr zw fylb."
alphapet = "abcdefghijklmnopqrstuvwxyz"
alphapet += alphapet    #This is a Trick. You can use it. 
new_string = ""
for i in s:#once edited:"for i in string:"
    if i not in alphapet:
        new_string += i
    else:
        new_string += alphapet[alphapet.index(i)+2]
print new_string

答案 2 :(得分:0)

问题是你只在其他地方而不是每次增加m。只要i是不在字母表中的字符,它就不再与字符串[m]相同。如果你设法m+=1,它将解决这个问题,但实际上你根本不需要m,因为你已经有i

你的代码中还有其他问题:str.replace没有修改字符串(字符串是不可变的)但返回修改后的字符串,你不会分配这个结果,所以它&#39没用了。

但即使replace()修改了你的字符串,你仍然有问题。

string = 'abcdef'
string = string.replace('a', 'c') # 'cbcdef'
string = string.replace('b', 'd') # 'cdcdef'
string = string.replace('c', 'e') # 'ededef' <- not what you want

因此,您需要在迭代时构造一个新的单独字符串,以执行您想要的操作。

或者您可以查看str.maketrans()方法。

答案 3 :(得分:0)

请参阅下面的修改版本。字符串替换应该特别保存回字符串string = string.replace(...)。正如其他人所指出的那样,m在原始代码中并没有增加。我使用enumerate来获取字符串字符和索引。

string="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
#=========this is the string on the site
alphapet="abcdefghijklmnopqrstuvwxyz"
#this is the alphapet ofcourse
x=""
y=0
z=""
m=0
#that was declaring variables

#temporarily convert the special cases
string = string.replace("y","Y")
string = string.replace("z","Z")

#the following is a loop which would go through string
#enumerate gets each string character(i) as well as its index (j)
for j,i in enumerate(string):
    if i in alphapet:
        x=string[j]
        y=alphapet.index(x)
        z=alphapet[y+2]
        string = string[:j]+string[j].replace(i,z)+string[j+1:]

#convert the special cases
string = string.replace("Y","a")
string = string.replace("Z","b")