Activity — Functions


Purpose:
  1. Practice problem solving
  2. Practice writing and using functions
  3. Understand named arguments
  4. Understand optional arguments
  5. Understand the difference between print and return
  6. Understand the concept of local and global variables
  7. Practice using expressions
  8. Understand data type and casting
  9. Practice formatting output and working with multiple/nested quotations

For this activity, you may work alone or with one partner. Do the following problems and write function(s) to solve the problems. Be sure to include docstrings describing what the function does.


Problem #1

Write a file (named awesomeshirtshop.py) with two functions: custom_shirt and get_total_order.

Note:
For simplicity, let's not worry about the length of the text.
For challenge, you may decide the maximum length of message and decide how to enforce the constraint.

Example invocations:

When you run awesomeshirtshop.py, nothing should happen. It defines functions, it does not run the function.

If in another file (let's call it driver1.py) you write the following:
(assume that the names of function parameters are size, msg, and location)
      
   import awesomeshirtshop

   awesomeshirtshop.custom_shirt("M", "I'm awesome", "back")
   awesomeshirtshop.custom_shirt("M", "I'm awesome")
   awesomeshirtshop.custom_shirt("M")
   awesomeshirtshop.custom_shirt(size="M", msg="I'm awesome", location="back")
   awesomeshirtshop.custom_shirt(size="M", msg="I'm awesome")
   awesomeshirtshop.custom_shirt(size="M")
   awesomeshirtshop.custom_shirt(msg="I'm awesome", size="M", location="back")
   awesomeshirtshop.custom_shirt(msg="I'm awesome", size="M")
   awesomeshirtshop.custom_shirt(size="M", location="back")

   print(awesomeshirtshop.get_total_order())

you should get the following output:
   Custom shirt size "M" with text "I'm awesome" printed on "back"
   Custom shirt size "M" with text "I'm awesome" printed on "front"
   Custom shirt size "M" with text "I'll get A+" printed on "front"
   Custom shirt size "M" with text "I'm awesome" printed on "back"
   Custom shirt size "M" with text "I'm awesome" printed on "front"
   Custom shirt size "M" with text "I'll get A+" printed on "front"
   Custom shirt size "M" with text "I'm awesome" printed on "back"
   Custom shirt size "M" with text "I'm awesome" printed on "front"
   Custom shirt size "M" with text "I'll get A+" printed on "back"
   total order: 9 shirts


Problem #2

Write a file (named awesomepizzashop.py) with two functions: make_pizza and get_total_order.

Note:
For simplicity, let's assume the toppings are spreaded evenly over the entire pizza.
For challenge, you may decide to allow half or full (or even light, regular, or extra amount of toppings) and decide how to enforce the constraint.

Example invocations:

When you run awesomepizzashop.py, nothing should happen. It defines functions, it does not run the function.

If in another file (let's call it driver2.py) you write the following:
(assume that the names of function parameters are size, top1, top2, and top3)
      
   import awesomepizzashop

   awesomepizzashop.make_pizza(12, "pepperoni", "mushroom", "pineapple")
   awesomepizzashop.make_pizza(16, "pepperoni", "mushroom")
   awesomepizzashop.make_pizza(20, "pepperoni")
   awesomepizzashop.make_pizza(20, top2="pepperoni")
   awesomepizzashop.make_pizza(size=12, top1="pepperoni", top2="mushroom", top3="pineapple")
   awesomepizzashop.make_pizza(size=16, top3="pepperoni", top1="mushroom", top2="pineapple")
   awesomepizzashop.make_pizza(20, top3="pepperoni", top1="mushroom")
   awesomepizzashop.make_pizza(16)

   print(awesomepizzashop.get_total_order())

you should get the following output:
   You order 12 inches pizza with pepperoni, mushroom, and pineapple
   You order 16 inches pizza with pepperoni, mushroom, and cheese
   You order 20 inches pizza with pepperoni, cheese, and cheese
   You order 20 inches pizza with cheese, pepperoni, and cheese
   You order 12 inches pizza with pepperoni, mushroom, and pineapple
   You order 16 inches pizza with mushroom, pineapple, and pepperoni
   You order 20 inches pizza with mushroom, cheese, and pepperoni
   You order 16 inches pizza with cheese, cheese, and cheese
   total order: 8 pizza