2016-06-23 1 views
0

アルファベットの2文字ですべての文字を置き換える必要があります。例えば、g~iおよびa~cである。私はここサブストリングが見つかりませんでした

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) 
+0

エラーの原因は何ですか? – nbryans

+1

y = alphapet.index(x) – Ohumeronen

+2

あなたの質問に含めるコードの中に 'substring not found 'という文字が表示されることはありません。 –

答えて

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 
+1

あなたは 'for i in string'を' for i in s'に変更するか、 's'の名前を変更する必要があります。 – roganjosh

+1

あなたはありがとう –

+1

それはまた、最後に "ab"を追加することによって解決することができます –

1

x=string[m] 

xを実行すると、インデックスmと同じになりますときには、句読点やスペースなどが含まれています。だから、

、あなたのalphapetには句読点やスペースがないため、エラーが発生します

y=alphapet.index(x) 

プログラムを実行しています。あなたがする必要がどのような

は句読点を処理するコードを変更し、またはalphapetに句読点を追加するのいずれかです。

+0

"英語のアルファベットの場合"に気づいました –

+0

@ islamzidan、私はまだテストしていませんが、私は 'string' [m]とは異なります。なぜなら、 'm'は' if i in alphapet'だけ増分され、 'i'は'for'ループの各繰り返し。 – JAW

0

問題は、毎回ではなく、代わりにmをインクリメントすることです。 iがalphapetにない文字であるとすぐに、それはもはやstring [m]と同じではありません。あなたはm+=1をdesindent場合は、その問題を解決しますが、あなたはすでにiを持っているので、実際にあなただけの、全くmを必要としません。

コードには他の問題があります。str.replaceは文字列を変更しません(文字列は不変です)が、変更された文字列を返します。この結果は割り当てられないので無駄です。 ()は、あなたの文字列を変更し交換する場合でも

しかし、あなたはまだ問題を抱えているD」。

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()メソッドを見ることができます。

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") 
関連する問題