"""
The coffee shop module contains functions and contains variables
important to implementing a coffee shop.
"""# Set some variables
shop_name ="Runestone Brew House"
coffee_sizes =["small","medium","large"]
coffee_roasts =["hot chocolate","light","medium","dark","espresso"]
This is a Python script named coffee_shop.py that contains three variables: shop_name, coffee_sizes, and coffee_roasts. The shop_name is a string, coffee_sizes is a list containing strings, and coffee_roasts is also a list containing strings.
import coffee_shop
# Output the information we know from the moduleprint("Welcome to", coffee_shop.shop_name)print("Available sizes:", coffee_shop.coffee_sizes)print("Available roasts:", coffee_shop.coffee_roasts)
This is a Python script named coffee_customer.py that imports our coffee_shop module, then prints out the information from that module.
We use dot notation to grab the shop_name, coffee_sizes, and coffee_roasts variables from the coffee_shop module. Then we print them out as parts of nice messages.
"""
The coffee shop module contains functions and contains variables
important to implementing a coffee shop.
"""# Set some variables
shop_name ="Runestone Brew House"
coffee_sizes =["small","medium","large"]
coffee_roasts =["hot chocolate","light","medium","dark","espresso"]deforder_coffee(size, roast):"""
Take an order from a user
:param size: a string containing one of the coffee_sizes
:param roast: a string containing one of the coffee_roasts
:return: a message about the coffee order
"""return"Here's your {} coffee roasted {}".format(size, roast)
The old file contents are present, but now there’s also an order_coffee function that takes two arguments, size and roast.
# Import the module with coffee_shop functionalityimport coffee_shop
# Output the information we know from the moduleprint("Welcome to", coffee_shop.shop_name)print("Available sizes:", coffee_shop.coffee_sizes)print("Available roasts:", coffee_shop.coffee_roasts)# Get some inputs from the user
order_size =input("What size coffee do you want? ")
order_roast =input("What roast do you want? ")# Send the order to the coffee shop module
shop_says = coffee_shop.order_coffee(order_size, order_roast)# Print out whatever it gave back to usprint(shop_says)
We added some lines to our coffee_customer script… Now after printing data nicely, coffee_customer asks the user for a size and a roast. These are the parameters required by our order_coffee function over in the coffee_shop module!
Call the order_coffee function with dot notation, just like retrieving variable values. The function call is the line that says shop_says = coffee_shop.order_coffee(order_size, order_roast). The function returns something, so we save that off in shop_says. The next line prints out whatever the shop said.
"""
The coffee shop module contains functions and contains variables
important to implementing a coffee shop.
"""# Set some variables
shop_name ="Runestone Brew House"
coffee_sizes =["small","medium","large"]
coffee_roasts =["hot chocolate","light","medium","dark","espresso"]deforder_coffee(size, roast):"""
Take an order from a user
:param size: a string containing one of the coffee_sizes
:param roast: a string containing one of the coffee_roasts
:return: a message about the coffee order
"""return"Here's your {} coffee roasted {}".format(size, roast)defadd_milk_please(fat_content):"""
Pretend like we're adding some milk to a coffee
:param fat_content: a string or integer containing the milkfat content
:return: a message about having added the milk
"""return"I've added the {}% milk".format(fat_content)
The new function is called add_milk_please and it takes one parameter - the fat_content. It returns a string explaining what happened.
# Import the module with coffee_shop functionalityimport coffee_shop
# Output the information we know from the moduleprint("Welcome to", coffee_shop.shop_name)print("Available sizes:", coffee_shop.coffee_sizes)print("Available roasts:", coffee_shop.coffee_roasts)# Get some inputs from the user
order_size =input("What size coffee do you want? ")
order_roast =input("What roast do you want? ")# Send the order to the coffee shop module
shop_says = coffee_shop.order_coffee(order_size, order_roast)# Print out whatever it gave back to usprint(shop_says)# See if the user wants to add milk
add_milk_response =input("Do you want to add milk (y/n)? ")# Convert the response to lowercase, then check for a "yes" answerif"y"in add_milk_response.lower():
milk_fat =input("What percent milk do you want added? ")
shop_says = coffee_shop.add_milk_please(milk_fat)# Print out whatever it gave back to usprint(shop_says)
That got fancy! We were just ordering coffee but now the user can choose to add milk! Selection is in a couple chapters, but if you read that code like english you’ll see what’s going on.
"""
The coffee shop module contains functions and contains variables
important to implementing a coffee shop.
"""# Set some variables
shop_name ="Runestone Brew House"
coffee_sizes =["small","medium","large"]
coffee_roasts =["hot chocolate","light","medium","dark","espresso"]deforder_coffee(size, roast):"""
Take an order from a user
:param size: a string containing one of the coffee_sizes
:param roast: a string containing one of the coffee_roasts
:return: a message about the coffee order
"""return"Here's your {} coffee roasted {}".format(size, roast)defadd_milk_please(fat_content):"""
Pretend like we're adding some milk to a coffee
:param fat_content: a string or integer containing the milkfat content
:return: a message about having added the milk
"""return"I've added the {}% milk".format(fat_content)defgive_tip(tip_amount):"""
Take a tip from the user, then be happy about it
:param tip_amount: the tip amount
:return: nothing
"""print("Thank you so much! We don't make a ton of money.")# Not having a "return" statement causes our function to return None
We added the give_tip function which takes one parameter, the tip_amount. We don’t actually do anything with that parameter… But if we were getting fancier with the coffee shop we might add it to the customer’s bill, we might print it out, or we might berate the customer for being too cheap… Here we just go ahead and blurt out a thanks to the user! Bein’ friendly is important.
# Import the module with coffee_shop functionalityimport coffee_shop
# Output the information we know from the moduleprint("Welcome to", coffee_shop.shop_name)print("Available sizes:", coffee_shop.coffee_sizes)print("Available roasts:", coffee_shop.coffee_roasts)# Get some inputs from the user
order_size =input("What size coffee do you want? ")
order_roast =input("What roast do you want? ")# Send the order to the coffee shop module
shop_says = coffee_shop.order_coffee(order_size, order_roast)# Print out whatever it gave back to usprint(shop_says)# See if the user wants to add milk
add_milk_response =input("Do you want to add milk (y/n)? ")# Convert the response to lowercase, then check for a "yes" answerif"y"in add_milk_response.lower():
milk_fat =input("What percent milk do you want added? ")
shop_says = coffee_shop.add_milk_please(milk_fat)# Print out whatever it gave back to usprint(shop_says)# They better give a tip...print("THAT'S GOOD COFFEE! Very good. Your brain is working again.")print("You better give a tip.")
tip_amount =input("Tip amount? ")
coffee_shop.give_tip(tip_amount)