Reputation: 779
Hi all :) I'm building a script to code text with a simple text rotation(ROT). The script works well, but I'm having a problem, it also rotates all symbols like [spaces,!,?,.] I'm working with ascii table to do it, what can i do to avoid rotating that type of characters?
def rot13(input,key): #Function to code a text with caeser chyper.
if key > 25:
key = 25
elif key < 2:
key = 2
finaltext = ''
for letter in input:
num = ord(letter)
if (num + key) > 122: #If the final number is greater than 122..
x = (num + key) - 122
finaltext += chr(x + ord('a') - 1)
elif((num + key <= 122)):
finaltext += chr(num + key)
print(finaltext)
Upvotes: 1
Views: 75
Reputation: 2203
before "rotating" your character, add a check to see whether or not it is alphanumeric:
if letter.isalpha():
# Do your thing
else:
finaltext += letter
Upvotes: 3
Reputation: 4086
Try this:
>>> import string
>>> letter = 'a'
>>> letter in string.letters
True
>>> letter = '.'
>>> letter in string.letters
False
Upvotes: 1