Question
Must be written in Python 3: The ord function in Python takes a character and return an integer that represents that character. It does not
Must be written in Python 3:
The ord function in Python takes a character and return an integer that represents that character. It does not matter what the integer representing the character actually is, but what matters is this:
ord('a') is 1 less than ord('b'), so that:
x=ord('a') thisLetter = x+1 # thisLetter is the ord('b')
This is a powerful fact that is used in encryption techniques, so data transferred over the web is 'deciphered so it is unreadable to others.
To decipher data, we take a string and change it into another string by adding a constant (called the key) to its ord value. See the book's Case study: word play.
To cipher and decipher data, we need two functions: one to take a string, and change it to something else. For this we need to use the ord function, and add the 'key' to result in a new string. For example, say x is one letter to be ciphered, and the key is 3. We can use:
Newx=ord(x)+3
Newx will be an integer. To find out what letter that integer represents you can use the chr function as in: actualLetter = chr(x)
Write a function named cipher that takes a string and a key (integer). The function ciphers the string into another string and returns the new string. Note that when we reach 'z', and we want to add the key, we must 'roll' into the alphabet one more time, hence ord('z')+3 should give us ord('c').
Write another function to decipher (do the opposite of the previous function), given a string and a key, it returns the deciphered string.
From main, write code to get a string, and an integer as input, then call the cipher function and print its output. Then call the decipher function and display its output. The decipher output should match the original string.
For help on this see the book's Case study: word play. Add comments as needed but make sure to add top level comments.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started