Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####method 1
- phrase = raw_input("Enter sentence to encrypt: ")
- shift = input("Enter shift value: ")
- encoded = ''
- for c in phrase:
- if c in 'abcdefghijklmnopqrstuvwxyz':
- encoded += chr((ord(c)-ord('a')+shift)%26 + ord('a'))
- elif c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
- encoded += chr((ord(c)-ord('A')+shift)%26 + ord('A'))
- else:
- encoded += c
- print "The encoded phrase is: " + encoded
- #### method 2:
- #Modularisation (Reforming aka refactoring the code to make functions to replace repeated code)
- #Using your method, one step at a time
- #mod=26 is a default value, this is what it's given if you don't give it a different value
- def shiftChar(char, base, shift, mod=26):
- char -= base
- char += shift
- char %= mod
- char +=base
- return char
- #Or bringing all of the steps into one line
- def shiftCharCondensed(char, base, shift, mod=26):
- return ((char-base)+shift)%mod+base
- phrase = raw_input("Enter sentence to encrypt: ")
- shift = input("Enter shift value: ")
- encoded = ''
- for c in phrase:
- ascii = ord(c)
- if 97 <= ascii < (97+26):
- ascii = shiftChar(ascii, 97, shift)
- elif 65 <= ascii < (65+26):
- ascii = shiftChar(ascii, 65, shift)
- c = chr(ascii)
- encoded += c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement