Question: write a function alphabet_position(letter), which receives a letter (that is, a string with only one alphabetic character) and returns the 0-based numerical position of that
write a function alphabet_position(letter), which receives a letter (that is, a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet.
Here are some example input parameter values, with the corresponding return values.
| letter | Return value |
|---|---|
| a | 0 |
| A | 0 |
| b | 1 |
| y | 24 |
| z | 25 |
| Z | 25 |
Note
The function should be case-insensitive.
Note
You can assume that your input will definitely be a letter. Dont worry about what might happen if somebody tries to use your function with an input parameter that is something other than a single letter, like alphabet_position("grandpa22!")
When (you think) you are finished, the best way to test your function is to fire up the Python shell in your terminal, and then import your file as a module:
$ python >>> import caesar >>> caesar.alphabet_position("a") 0 >>> caesar.alphabet_position("E") 4 # ... etc Warning
It is important that you test your function to make sure it works before moving on. This practice of testing each little piece of your code in isolation before trying to put all the pieces together will save you a lot of time and headaches.
rotate_character
Next, write another helper function rotate_character(char, rot) which receives a character char(that is, a string of length 1), and an integer rot. Your function should return a new string of length 1, the result of rotating char by rot number of places to the right.
Here are some example input values, with the corresponding return values.
| char | rot | Return value |
|---|---|---|
| a | 13 | n |
| a | 14 | o |
| a | 0 | a |
| c | 26 | c |
| c | 27 | d |
| A | 13 | N |
| z | 1 | a |
| Z | 2 | B |
| z | 37 | k |
| ! | 37 | ! |
| 6 | 13 | 6 |
Note
The upper or lower case of the letter should be preserved. For example, rotate_character("A", 13) results in "N", rather than "n"
Note
For non-alphabetical characters, you should ignore the rot argument and simply return charuntouched. For example, see "!" and "6" in the table above.
Ok, go code like a champ.
Hint
You should make use of your own alphabet_position function! If feeling confused, you may want to re-read about how Functions Can Call Other Functions
Warning
Once again, before moving on to the next stage, make sure to test rotate_character with a wide variety of input values (beyond just the examples we provide).
encrypt
Now lets get to the heart of the matter. Write one more function called encrypt(text, rot), which receives as input a string and an integer. This is just like the rot13 function, but instead of hardcoding the number 13, your function should receive a second argument, rot which specifies the rotation amount. Your function should return the result of rotating each letter in the text by rot places to the right.
Here are some example input values, with the corresponding return values:
| text | rot | Return value |
|---|---|---|
| a | 13 | n |
| abcd | 13 | nopq |
| LaunchCode | 13 | YnhapuPbqr |
| LaunchCode | 1 | MbvodiDpef |
| Hello, World! | 5 | Mjqqt, Btwqi! |
Note
The text argument might contain non-alphabetic characters (spaces, numbers, symbols). You should leave these as they are.
Hint
You should make use of your own rotate_character function (which should make it very easy to satisfy the condition above).
Warning
Dont forget to test!
Make It Interactive
Youre almost done with Caesar! The last step is simply to write some print and input statements so the user can run your program from the terminal. Remember, you should ask the user for their message and rotation amount, and then print the encrypted message:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
