SlideShare a Scribd company logo
#Marie Dimond
#11-12-2015
#dimond_caesar_cipher.py
#Caesar's Cipher using lists and while loops, encodes a message
#Constants
#Uppercase alpabet list for uppercase letters
UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#Uppercase alpabet list for uppercase letters
LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz"
#Repeats game until user says no
while True:
#Get user message
def getMessage():
print("Enter your message:")
return input()
#Get user key
def getKey():
key = 0
while True:
print ("Enter the key number (1-26):")
try:
key = int(input())
except ValueError:#Checks if value is interger
print("Please enter a valid number.")
continue
if key >= 1 and key <= 26:#Between 1 and 26
print("Key accepted")#Breadcrumb
return key
break
else:
print ("Please enter a number between 1 and 26")
def getMode():
while True:# <=== Need this for repition
print("Do you wish to encrypt (e) or decrypt (d) a message?")
mode = input().lower()
if mode == 'd':
print("Decrypt") #Breadcrumb
return mode #mode 'd'
print()
break #Exit
elif mode == 'e':
print("Encrypt")#Breadcrumb
return mode #mode 'e'
print()
break
else:
print("Please enter (e) or (d).")#error
#Making the funtion returns equal the variables
message = getMessage()
print()
key = getKey()
print()
mode = getMode()
print()
#Easy to read lise for reader's convience
print("Message: " + str(message))
print("Key: " + str(key))
print("Mode: " + str(mode))
print()
#Translate messages using two lists and indexing
def Translation(message, key, mode):
cipher = " "
if mode [0] == "d":
key = -key
for c in message:
if c in UPPER_ALPHABET:
cipher += UPPER_ALPHABET[(UPPER_ALPHABET.index(c)+key)
%(len(UPPER_ALPHABET))]
elif c in LOWER_ALPHABET:
cipher += LOWER_ALPHABET[(LOWER_ALPHABET.index(c)+key)
%(len(LOWER_ALPHABET))]
else:
cipher = cipher + c #Does nothing to empty spaces
return cipher
#Prints translated message
print ("Your translated text is:")
print()
print(Translation(message, key, mode))
print()
print("Do you wish to encrypt or decrypt another message?")
print("(y) Yes, (n) No")
mode = input().lower()
if mode == 'y':
print("Great!")
print()
continue
elif mode == 'n':
print("Thank you for playing.")
break
else:
print("Please enter (y) or (n).")
print()
#Notes
#Sources:
#https://www.youtube.com/watch?v=WXIHuQU6Vrs
#http://inventwithpython.com/hacking/chapter17.html(Free online book)
#https://inventwithpython.com/chapter14.html
#https://courses.cs.washington.edu/courses/cse142/11au/python/week4/encrypt.py
#Above used for cipher = cipher + c
#Glossary
#Breadcrumb = Developement tool to check functions,
#can be removed post-beta test

More Related Content

Viewers also liked

IMS-PSTN Interworking Flow
IMS-PSTN Interworking FlowIMS-PSTN Interworking Flow
IMS-PSTN Interworking Flow
Houman Sadeghi Kaji
 
25 Zadar Events | Summer 2016 | part 3
25 Zadar Events | Summer 2016 | part 325 Zadar Events | Summer 2016 | part 3
25 Zadar Events | Summer 2016 | part 3
Plan 5 d.o.o.
 
Hacker's jargons
Hacker's jargonsHacker's jargons
Strategi Pelaksanaan Jiwa - Perilaku Kekerasan
Strategi Pelaksanaan Jiwa - Perilaku KekerasanStrategi Pelaksanaan Jiwa - Perilaku Kekerasan
Strategi Pelaksanaan Jiwa - Perilaku Kekerasan
Yusuf Saktian
 
Pengawasan atau supervisi dalam lembaga pendidikan islam
Pengawasan atau supervisi dalam lembaga pendidikan islamPengawasan atau supervisi dalam lembaga pendidikan islam
Pengawasan atau supervisi dalam lembaga pendidikan islam
Lutfy Nikmah
 
Strategi Pelaksanaan Jiwa - Isolasi Sosial
Strategi Pelaksanaan Jiwa - Isolasi SosialStrategi Pelaksanaan Jiwa - Isolasi Sosial
Strategi Pelaksanaan Jiwa - Isolasi Sosial
Yusuf Saktian
 
Laporan Pendahuluan Jiwa - Perilaku Kekerasan
Laporan Pendahuluan Jiwa - Perilaku KekerasanLaporan Pendahuluan Jiwa - Perilaku Kekerasan
Laporan Pendahuluan Jiwa - Perilaku Kekerasan
Yusuf Saktian
 
Makalah MSDM Strategik
Makalah MSDM StrategikMakalah MSDM Strategik
Makalah MSDM Strategik
Melda Ferosa
 
Jazz Festivals in Europe
Jazz Festivals in EuropeJazz Festivals in Europe
Jazz Festivals in Europe
Plan 5 d.o.o.
 

Viewers also liked (9)

IMS-PSTN Interworking Flow
IMS-PSTN Interworking FlowIMS-PSTN Interworking Flow
IMS-PSTN Interworking Flow
 
25 Zadar Events | Summer 2016 | part 3
25 Zadar Events | Summer 2016 | part 325 Zadar Events | Summer 2016 | part 3
25 Zadar Events | Summer 2016 | part 3
 
Hacker's jargons
Hacker's jargonsHacker's jargons
Hacker's jargons
 
Strategi Pelaksanaan Jiwa - Perilaku Kekerasan
Strategi Pelaksanaan Jiwa - Perilaku KekerasanStrategi Pelaksanaan Jiwa - Perilaku Kekerasan
Strategi Pelaksanaan Jiwa - Perilaku Kekerasan
 
Pengawasan atau supervisi dalam lembaga pendidikan islam
Pengawasan atau supervisi dalam lembaga pendidikan islamPengawasan atau supervisi dalam lembaga pendidikan islam
Pengawasan atau supervisi dalam lembaga pendidikan islam
 
Strategi Pelaksanaan Jiwa - Isolasi Sosial
Strategi Pelaksanaan Jiwa - Isolasi SosialStrategi Pelaksanaan Jiwa - Isolasi Sosial
Strategi Pelaksanaan Jiwa - Isolasi Sosial
 
Laporan Pendahuluan Jiwa - Perilaku Kekerasan
Laporan Pendahuluan Jiwa - Perilaku KekerasanLaporan Pendahuluan Jiwa - Perilaku Kekerasan
Laporan Pendahuluan Jiwa - Perilaku Kekerasan
 
Makalah MSDM Strategik
Makalah MSDM StrategikMakalah MSDM Strategik
Makalah MSDM Strategik
 
Jazz Festivals in Europe
Jazz Festivals in EuropeJazz Festivals in Europe
Jazz Festivals in Europe
 

Example of Python Code

  • 1. #Marie Dimond #11-12-2015 #dimond_caesar_cipher.py #Caesar's Cipher using lists and while loops, encodes a message #Constants #Uppercase alpabet list for uppercase letters UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #Uppercase alpabet list for uppercase letters LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz" #Repeats game until user says no while True: #Get user message def getMessage(): print("Enter your message:") return input() #Get user key def getKey(): key = 0 while True: print ("Enter the key number (1-26):") try: key = int(input()) except ValueError:#Checks if value is interger print("Please enter a valid number.") continue if key >= 1 and key <= 26:#Between 1 and 26 print("Key accepted")#Breadcrumb return key break else: print ("Please enter a number between 1 and 26") def getMode(): while True:# <=== Need this for repition print("Do you wish to encrypt (e) or decrypt (d) a message?") mode = input().lower() if mode == 'd': print("Decrypt") #Breadcrumb return mode #mode 'd' print() break #Exit
  • 2. elif mode == 'e': print("Encrypt")#Breadcrumb return mode #mode 'e' print() break else: print("Please enter (e) or (d).")#error #Making the funtion returns equal the variables message = getMessage() print() key = getKey() print() mode = getMode() print() #Easy to read lise for reader's convience print("Message: " + str(message)) print("Key: " + str(key)) print("Mode: " + str(mode)) print() #Translate messages using two lists and indexing def Translation(message, key, mode): cipher = " " if mode [0] == "d": key = -key for c in message: if c in UPPER_ALPHABET: cipher += UPPER_ALPHABET[(UPPER_ALPHABET.index(c)+key) %(len(UPPER_ALPHABET))] elif c in LOWER_ALPHABET: cipher += LOWER_ALPHABET[(LOWER_ALPHABET.index(c)+key) %(len(LOWER_ALPHABET))] else: cipher = cipher + c #Does nothing to empty spaces return cipher #Prints translated message print ("Your translated text is:") print() print(Translation(message, key, mode)) print() print("Do you wish to encrypt or decrypt another message?") print("(y) Yes, (n) No")
  • 3. mode = input().lower() if mode == 'y': print("Great!") print() continue elif mode == 'n': print("Thank you for playing.") break else: print("Please enter (y) or (n).") print() #Notes #Sources: #https://www.youtube.com/watch?v=WXIHuQU6Vrs #http://inventwithpython.com/hacking/chapter17.html(Free online book) #https://inventwithpython.com/chapter14.html #https://courses.cs.washington.edu/courses/cse142/11au/python/week4/encrypt.py #Above used for cipher = cipher + c #Glossary #Breadcrumb = Developement tool to check functions, #can be removed post-beta test