Python text shift function

I am writing code so that you can shift the text in two places alphabetically: "ab cd" should become "cd ef". I am 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 need to somehow change the letter into numbers? But I thought I already did it?

+4
source share
4 answers

It looks like you are doing cesar-cipher encryption, so you can try something like this:

 strs = 'abcdefghijklmnopqrstuvwxyz' #use a string like this, instead of ord() def shifttext(shift): inp = raw_input('Input text here: ') data = [] for i in inp: #iterate over the text not some list if i.strip() and i in strs: # if the char is not a space "" data.append(strs[(strs.index(i) + shift) % 26]) else: data.append(i) #if space the simply append it to data output = ''.join(data) return output 

output:

 In [2]: shifttext(3) Input text here: how are you? Out[2]: 'krz duh brx?' In [3]: shifttext(3) Input text here: Fine. Out[3]: 'Flqh.' 

strs[(strs.index(i) + shift) % 26] : the line above means looking for the index of the character i in strs , and then add the shift value to it. Now, at the last value (index + shift), apply% 26 to get the shifted index. This shifted index, when passed to strs[new_index] gives the desired shifted character.

+5
source

You iterate over a list of characters, and i is a character. Then you try to save this back to data using the i character as an index. This will not work.

Use enumerate() to get indexes and values:

 def shifttext(shift): input=raw_input('Input text here: ') data = list(input) for i, char in enumerate(data): data[i] = chr((ord(char) + shift) % 26) output = ''.join(data) return output 

You can simplify this with a generator expression:

 def shifttext(shift): input=raw_input('Input text here: ') return ''.join(chr((ord(char) + shift) % 26) for char in input) 

But now you will notice that your % 26 will not work; ASCII code points begin after 26:

 >>> ord('a') 97 

You need to use ord('a') to use the module; Subtraction puts your values ​​in the range of 0-25, and you add them again:

  a = ord('a') return ''.join(chr((ord(char) - a + shift) % 26) + a) for char in input) 

but this will only work for lowercase letters; which may be fine, but you can force it by lower input input:

  a = ord('a') return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in input.lower()) 

If we then proceed to the request to exit the function in order to focus it on doing one job, it will look like this:

 def shifttext(text, shift): a = ord('a') return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in text.lower()) print shifttext(raw_input('Input text here: '), 3) 

and using this in the online tooltip, I see:

 >>> print shifttext(raw_input('Input text here: '), 3) Input text here: Cesarsalad! fhvduvdodgr 

Of course, punctuation is now taken. Last revision, now only letter offset:

 def shifttext(text, shift): a = ord('a') return ''.join( chr((ord(char) - a + shift) % 26 + a) if 'a' <= char <= 'z' else char for char in text.lower()) 

and get:

 >>> print shifttext(raw_input('Input text here: '), 3) Input text here: Ceasarsalad! fhdvduvdodg! 
+8
source

Martijn's answer is great. Here is another way to achieve the same:

 import string def shifttext(text, shift): shift %= 26 # optional, allows for |shift| > 26 alphabet = string.lowercase # 'abcdefghijklmnopqrstuvwxyz' (note: for Python 3, use string.ascii_lowercase instead) shifted_alphabet = alphabet[shift:] + alphabet[:shift] return string.translate(text, string.maketrans(alphabet, shifted_alphabet)) print shifttext(raw_input('Input text here: '), 3) 
+2
source

It is easier to write the direct function shifttext(text, shift) . If you want to receive an invitation, use Python interactive mode python -i shift.py

 > shifttext('hello', 2) 'jgnnq' 
+1
source

All Articles