Presentation is loading. Please wait.

Presentation is loading. Please wait.

String X=“Python Rocks!” X.index(“O”).

Similar presentations


Presentation on theme: "String X=“Python Rocks!” X.index(“O”)."— Presentation transcript:

1 String X=“Python Rocks!” X.index(“O”)

2 Character Encoding ASCII code
A – 65; B – 66; … ; Z – 90 >>> ord(‘A’) # ASCII value or order of letter >>> chr(65) # opposite of ord()

3 Cryptography Encoding and Decoding Messages

4 Translation Cipher Replace each character by ord(character)+constant
A B C D E F …. X Y Z 26 or over -> wrap around

5 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)

6 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)

7 Encryption Key

8 Vigenere Square

9 A Card Game Computer draws a card for me and one for you
Wins whichever has a higher face value

10 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”))

11 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 two programs to


Download ppt "String X=“Python Rocks!” X.index(“O”)."

Similar presentations


Ads by Google