Assignment 3
Defining and Using Functions
Submit before 11:30 PM Friday September 29

Overview

You will define several functions. The assignment makes the important distinction between functions that use arguments (aka parameters) and return values, and functions that are interactive (use input and print).

Some of the functions involve looping for a specific number of times. Read about the range function for how to create these loops.

Python Functions

Write and test Python functions that do the following:

  1. Complete the varied wishes function from lab 3 if you haven't yet. Include it with your assignment 3 submission.
  2. 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?
    

    Here's an example run from the interactive shell:

       >>> meditate(3)
       May I be happy?
       May I be at peace?
       May I live with ease?
       May I be free from suffering?
    
       May I be happy?
       May I be at peace?
       May I live with ease?
       May I be free from suffering?
    
       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.

  3. Write a function called cost_per_sq_inch. It should take two arguments: the first is a cost in dollars. The second argument is a diamater in inches. The function should returnthe cost in dollars per square inch. Here are example runs:
        >>> cost_per_sq_inch(12.50, 16)
        0.062169899645271615
        >>> 
    
        >>> cost_per_sq_inch(9.25, 12)
        0.08178795686666844
        >>>
      
  4. Write an interactive Python function called pizza(). This function does not take any arguments, nor does it return an value. Instead, the pizza function prompts the user for the costs and the diameters of two pizzas. It should call the cost_per_sq_inch function to calculate the relative costs of both pizzas, report the relative costs and indicate which pizza is the better value. Here is an example run:
        pizza()
        What is the cost of the first pizza? 12
        What is the diameter of the first pizza? 8
        What is the cost of the second pizza? 20
        What is the diameter of the second pizza? 13
        The relative cost for the first pizza is 0.238732414637843
        The relative cost for the second pizza is 0.15067923606333286
        The second pizza is the better deal!
        >>>
      

    Note that the pizza function uses input statements and print statements. It should not have any code in it that calculates area or performs division. Instead, it should call your cost_per_sq_inch function.

For each function, make sure that you have included a doc string!

Deliverables

Create a text file called assn3.txt that contains the following:

  1. A statement that summarizes your completion of the assignment. It should contain the following information:
    • Any help or resources that you used in completing the assignment.
    • A summary of your experience possibly including your approach, difficulties and time you spent.
  2. For each of your Python functions:
    1. A listing of the function
    2. Running examples that demonstrate that your function works correctly

Submit your assn3.txt file to D2L.