String X=“Python Rocks!” X.index(“O”)
Character Encoding ASCII code A – 65; B – 66; … ; Z – 90 http://www.asciitable.com/ >>> ord(‘A’) # ASCII value or order of letter >>> chr(65) # opposite of ord()
Cryptography Encoding and Decoding Messages
Translation Cipher Replace each character by ord(character)+constant A B C D E F …. X Y Z 0 1 2 3 4 5 …. 23 24 25 +5 5 6 7 8 9 10 … 26 or over -> wrap around
EncoderDecoder.py (p.99) message = input(“Enter a message to encode: \n”) message = message.upper() # make all uppercase cipher = “” for letter in message: if letter.isupper(): val = ord(letter)+13 newlet = chr(val) if not newlet.isupper(): newlet = chr(val-26) cipher = cipher + newlet print(“Cipher text: “, cipher)
Update with Modulo Function EncoderDecoder.py Update with Modulo Function message = input(“Enter a message to encode: \n”) message = message.upper() cipher = “” for letter in message: if letter.isupper(): val = ord(letter)+13 newlet = chr(val) if not newlet.isupper(): newlet = chr(val-26) cipher = cipher + newlet print(“Cipher text: “, cipher) message = input(“Enter a message to encode: \n”) message = message.upper() cipher = “” for letter in message: cipher = cipher + newlet print(“Cipher text: “, cipher)
Encryption Key
Vigenere Square
A Card Game Computer draws a card for me and one for you Wins whichever has a higher face value
HighCard.py (p. 125) import random faces = [“two”,”three”,”four”,”five”,”six”,”seven”,”eight”,”nine”,”ten”,”jack”,”queen”,”kin g”,”ace”] keep_going=1 while keep_going>0: my_face = random.choice(faces) your_face = random.choice(faces) if faces.index(my_face) > faces.index(your_face): print (“I win!”) elif faces.index(my_face)<faces.index(your_face): print(“You win!”) else: print(“A tie!”) keep_going=eval(input(“Enter 0 to stop, 1 to continue”))
HW 3 Due next Wed (10/14) 10:00 AM Do two problems Chap. 5 #2: User-defined keys (p. 102) Instead of constant 13 to translate, use a number a user enters in the keyboard Use the modulo operator to replace a letter by a cipher letter Chap. 6 #3: War Email two programs to 2015fall91.100@gmail.com