1

ファイルを受け取り、Viginère 暗号を使用してエンコードするプログラムを作成しようとしています。インデックスで少し問題が発生しました。私は自分の文字列を定義textし、次のalphabetようにしました:

import string

alphabet = string.ascii_lowercase
ciphertext = open("black_hole.txt","r")
ciphertext = ciphertext.read()
text = ""

for i in range(len(ciphertext)):
    if ciphertext[i].isalpha() == True:
        text = text + ciphertext[i]

これを for ループに書き込もうとすると、問題が発生します。

for i in range(len(text)):
    print(alphabet.index(text[i]))

ValueError「部分文字列が見つかりません」が表示されます。text[i] は常に文字と文字列であるため、これは奇妙です。

この質問を十分に明確に提示していない場合はお知らせください。

4

2 に答える 2

1
for i in range(len(text)):
    print(alphabet.index(text.lower()[i]))

lower() を追加するだけで機能します

于 2015-06-03T17:58:13.560 に答える