Question: 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

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 'ciphered 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 each of its letters' 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. The function returns that string in a ciphered form by using the ord of the first letter of the string to cipher each letter including the first letter. Hence for abc, use the ord of 'a', and add it to the ord of 'a' and convert that result to a character use the chr function. This character should be concatenated to the same action on the letter b and so on. Hence the function returns:

chr(ord('a')+ord('a')) + chr(ord('a')+ord('b')) + chr(ord('a')+ord('c')). Obviously you need a loop to iterate on each letter.

Write another function to decipher (do the opposite of the previous function), given a string and returns the deciphered string. Obviously, the first letter's ord is halved to find the first letter, and that value is used to decipher the remaining letters.

From main, write code to get a string, 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!