I’m writing code so you can shift text two places along the alphabet: ‘ab cd’ should become ‘cd ef’. I’m using Python 2 and this is what I got so far:
def shifttext(shift):
input=raw_input('Input text here: ')
data = list(input)
for i in data:
data[i] = chr((ord(i) + shift) % 26)
output = ''.join(data)
return output
shifttext(3)
I get the following error:
File "level1.py", line 9, in <module>
shifttext(3)
File "level1.py", line 5, in shifttext
data[i] = chr((ord(i) + shift) % 26)
TypError: list indices must be integers, not str
So I have to change the letter to numbers somehow? But I thought I already did that?
Looks you’re doing cesar-cipher encryption, so you can try something like this:
output:
strs[(strs.index(i) + shift) % 26]: line above means find the index of the characteriinstrsand then add the shift value to it.Now, on the final value(index+shift) apply %26 to the get the shifted index. This shifted index when passed tostrs[new_index]yields the desired shifted character.