Search code examples
pythonpython-3.xencryptionindicesvigenere

Trouble with for loops in Python 3: getting the index in string2 of an element from string1


I am trying to write a program that will take a file and encode it using the Viginère cipher. I've run into a little bump with indices. I've defined my strings text and alphabet like this:

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]

My trouble starts when I try to write this for loop:

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

I get the ValueError "substring not found". I find this strange since text[i] is always a letter as well as a string.

Please let me know if I haven't posed this question clearly enough!


Solution

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

    just add lower() and it will work