Home Schedule

Lab 5: Indicies and Range (10 pts)

Setup

  1. Locate your "CS102" folder
  2. Create a folder inside "CS102" called "lab05"
  3. Download lab05.py
  4. Move the "lab05.py" file into your lab05 folder
  5. Open "lab05.py" with Thonny
  6. READ THE COMMENTS
  7. Submit lab05.py to Moodle when you are finished

Now that we are writing functions, make sure your work today (and all future assignments) follows our class Python style guidelines and you include function comments. Be careful of your indentation!

Function 1:

Create a function named in_both which takes two strings, string1 and string2 as parameters and returns a string containing letters which occur in both strings. The returned string should NOT have repeating letters. If no letters are shared by the two strings, then an empty string is printed ('').

In the main function, call in_both and print the result to test it works. Use the following example function calls and verify them against the example output below.

Main Function Calls:

Expected Output (respectively):

Function 2:

Create a function named get_first_half that returns the first half of a string. The function should take one string parameter named string_value and return a new string made up of the first half of string_value. HINT You might need to do something special if the length of the string is odd

In the main function, call get_first_half and print the results to test it works. Use the following example function calls and verify them against the example output below.

Main Function Calls:

Example Printed Output (respectively):

Function 3:

You will create a function named decode that can decode a secret string (similar to Chapter 3.2.5 in your book). Your function will take two string parameters secret_word and key and will return a new string with the decoded message. NOTE: If secret_word contains spaces DO NOT try to decode them, simply add them to the decoded message.

In the main function, call decode and print the results to test it works. Use the following example function calls and verify them against the example output below.

Main Function Calls:

Example Printed Output (respectively):