Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''This function takes a message string, and encrypts it based on a
- Ceaser Shift to the nth position'''
- def rot(msg,n):`
- #Set the alphabet, include spaces
- alphabet = "abcdefghijklmnopqrstuvwxyz "
- #Create a key - Here, I am splitting the original string at the nth index,
- #then tacking on the rest of it to the end
- rotation=alphabet[n:]+alphabet[:n]
- #Initialize your cipertext variable as blank string
- newstring=''
- #Iterate through message string, character by character
- for i in msg:
- #If the character is a space, we will simply add a space instead of shifting
- if i == ' ':
- newstring+=i
- #If the character is not a space, take the
- #character's index in the alphabet, and add its correspondent to the cipher string
- else:
- newstring+=rotation[alphabet.index(i)]
- return newstring
- #Testing...
- def testfunctions():
- msg= input('Message: ')
- #Needs to take an integer value.
- #Input makes it a string, so need int() to convert
- n = int(input('Rotation: '))
- print('Rot',n,': ',rot(msg,n))
Add Comment
Please, Sign In to add comment