CS1315: Introduction to Media Computation
Download
Report
Transcript CS1315: Introduction to Media Computation
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 ….
0 1 2 3 4 5 ….
+5
5 6 7 8 9 10 …
26 or over -> wrap around
X Y Z
23 24 25
EncoderDecoder
# Ask what message you want to encrypt, and name it ‘msg’
msg = msg.upper()
# make all uppercases
# Initially, set ‘cipher’ is blank
for letter in msg:
# get the order of the letter and add 5
# find a char for the new order, but wrap around 26
# concatenate the new char to cipher
print(“Cipher text: “, cipher)
Cipher.py
# Ask what message you want to encrypt, and name it ‘msg’
msg = msg.upper()
# make all uppercases
# Initially, set ‘cipher’ is blank
for letter in msg:
# get the order of the letter and add 5
# find a char for the new order, but wrap around 26
# concatenate the new char to cipher
print(“Cipher text: “, cipher)
EncoderDecoder.py (p.99)
message = input(“Enter a message to encrypt: \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)
Update with Modulo Function
message = input(“Enter a
message to encode: \n”)
message = input(“Enter a message to
encode: \n”)
message = message.upper()
message = message.upper()
cipher = “”
cipher = “”
for letter in message:
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)
cipher = cipher + newlet
print(“Cipher text: “, cipher)
Encryption Key
Vigenere Square
Eliza
A word game to simulate Gestalt psychotherapist
Hello. Welcome to therapy. What is your name ? Tim
Well Tim, What can we do for you today? I am writing
a book on Python
Tell me more. Do you know Python?
Why do you want to know?
Eliza
Lab_0226
Modify Eliza program
Add the main (control) program that repeated ask
for your response and call function runEliza()
Add at least one ‘elif’ case
Add more cases to handle
Email to [email protected]