Assignment 4
Functions, Lists and Loops
Submit before 11:30 PM Saturday April 27
Overview
You will write multiple Python programs or functions that practice the use of loops and lists.
Python Functions
Write and test Python functions that do the following:
- Auto-meditate: write a function called meditate that takes a positive whole number as a parameter. The number indicates how many times to repeatedly print the following loving-kindness meditation text:
May I be happy? May I be at peace? May I live with ease? May I be free from suffering?
Feel free to adapt the function to fit your own (or your computer's) preferences. Consider adding a delay between each presentation.
- String stats: write a function called stats that takes a List of strings as a parameter. The function should print out a report with the following statistics:
- The number of words in the list.
- The longest word (just provide the first if there is more than one).
- The average word length.
The function must use a for-loop and keep a running total.
Here is an example run:
>>> stats(["cow", "aardvark", "sheep", "owl", "horse"]) The number of words is 5 The longest word is aardvark The average word length is 4.8 >>>
- Write a function called coin_tosses. It takes one paramater, an integer, which specifies the number of coin_tosses that the function simulates. It should then print out a series of Hs and Ts showing the outcome, all on the same line. Note: to have multiple prints to the same line, you will need to specify the optional end character to be the empty string.
- Automated Kumbaya: Write a function called song that has one parameter, a list of activities (e.g. singing, praying, crying). Using the actions in the lists, it should printed stanzas of Kumbaya. Feel free to modify or simplify the verses to your taste (or religious tradition), provided that each stanza has multiple uses of each action.
Deliverables
Create a summary file called assn4 (pdf or txt) that contains the following:
- A statement that summarizes your completion of the assignment. Discuss your experience. Note any help or collaboration.
- For each of your Python code problems:
- A listing of the code (copy the code in the py file and past it in the assn1 file)
- Running examples that demonstrate that your code works correctly (copy the text that shows your py file running and paste it in the assn4 file)
Submit your assn4.pdf (or assn4.txt) file to D2L.