I'm trying to get the location of all the letters of an input "text" from a list "alphabet". <- Thats the part I'm having trouble with.
For some context: after those letters are stored, the list aplphabet will shift a given number of places. Then our stored locations can be retrieved and we can print out an encoded message.
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
text_len = len(text)
i = text_len
text_catch = []
while text_len != 0:
while i != 0:
print(alphabet.index(text[i - 1]))
for i in text:
text_catch.append(alphabet.index(text[i-1]))
i -= 1
text_len -= 1
I am using the word hello as an imput. When I remove
for i in text:
text_catch.append(alphabet.index(text[i-1]))
I get each location of the letters hello printed out. I'm having trouble storing those locations to another list. (type error: unsupported opperand type(s) for -: 'str' and 'int'
I tried
for i in text:
text_catch.append(alphabet.index(text[i-1]))
so that every time it looped, it would add the location alphabet.index(text[i-1] to list text_catch. I got a type error. Very new to this and honestly, still way over my head.
CodePudding user response:
The type error comes from using i as an index integer (i = len(text)) and as a string (for i in text); here's a fixed version of your code:
i = len(text)
text_catch = []
while i != 0:
print(alphabet.index(text[i - 1]))
text_catch.append(alphabet.index(text[i-1]))
i -= 1
Note that you don't need indexes, nor do you need to loop through text in reverse; you can simply do:
for letter in text:
print(alphabet.index(letter))
text_catch.append(alphabet.index(letter))
And there actually are far quicker methods to do the same (look at the ord
and chr
functions). For example:
def encode(text, shift):
return ''.join([chr(((ord(letter)-97) shift)& 97) for letter in text])
def decode(text, shift):
return encode(text,-shift)
CodePudding user response:
Instead of using nested loops, you could try out a simpler approach to encode/decode text based on the user's input.
For instance, you could try something like:
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
processed_text = ''
for character in text:
idx = alphabets.index(character)
# choose the operation
if direction == 'encode':
shifted_idx = idx shift
else:
shifted_idx = idx - shift
# wrap-around indexes which are greater than 25 to make sure you get an alphabet from the start of the alphabets instead
shifted_idx %= 26
# append alphabet to your output string
processed_text = alphabets[shifted_idx]
print(processed_text)