What is a Coffee Machine?
A Coffee Machine is an application that allows users to select a type of coffee and calculates the necessary ingredients and cost based on the selected type of coffee. The user makes a coffee selection, and the machine prepares the coffee according to the user's choice. Such applications are ideal for practicing basic programming and user interaction concepts.
Project Objectives and Student Benefits
Through this project, students will reinforce their ability to develop simple and functional programs using basic Python knowledge. Additionally, they will gain experience in user interaction and data processing.
Project Outputs
Create a coffee machine application using basic Python structures and control flows.
Collect data from users and manage coffee preparation processes by processing this data.
Write more modular and readable code using functions.
Step-by-Step Tasks of the Project
Define coffee options and ingredients.
Collect coffee selection from the user.
Check the necessary ingredients and prepare the coffee based on the selected type.
Print the result.
Make the code more modular using functions.
Solution Without Functions
Step 1: Define Coffee Options and Ingredients
In the first step, coffee options and ingredients will be defined.
# Define coffee options and ingredients
coffees = {
"espresso": {"water": 50, "milk": 0, "coffee": 18, "price": 1.5},
"latte": {"water": 200, "milk": 150, "coffee": 24, "price": 2.5},
"cappuccino": {"water": 250, "milk": 100, "coffee": 24, "price": 3.0}
}
stock = {"water": 1000, "milk": 500, "coffee": 100, "money": 0}
This code snippet defines the coffee options and necessary ingredients. It also defines the stock of ingredients.
Step 2: Collect Coffee Selection from the User
In the second step, the coffee selection will be collected from the user.
# Collect coffee selection from the user
selection = input("Which coffee would you like? (espresso/latte/cappuccino): ").lower()
This code snippet collects the coffee selection from the user.
Step 3: Check the Necessary Ingredients and Prepare the Coffee Based on the Selected Type
In the third step, the necessary ingredients will be checked, and the coffee will be prepared based on the selected type.
# Check the necessary ingredients and prepare the coffee based on the selected type
if selection in coffees:
enough_ingredients = True
for ingredient in coffees[selection]:
if ingredient != "price" and coffees[selection][ingredient] > stock[ingredient]:
print(f"Not enough {ingredient}.")
enough_ingredients = False
break
if enough_ingredients:
for ingredient in coffees[selection]:
if ingredient != "price":
stock[ingredient] -= coffees[selection][ingredient]
stock["money"] += coffees[selection]["price"]
print(f"{selection} is ready! Enjoy.")
else:
print("Invalid selection.")
This code snippet checks the necessary ingredients for the selected coffee, prepares the coffee if enough ingredients are available, and updates the stock.
Step 4: Print the Result
In the fourth step, the result will be printed.
# Print the result
print("Remaining stock:")
for ingredient, amount in stock.items():
print(f"{ingredient}: {amount}")
This code snippet prints the updated stock.
Solution with Functions
Step 1: Function to Define Coffee Options and Ingredients
In the first step, coffee options and ingredients will be defined.
def define_coffee_options_and_stock():
"""Defines coffee options and stock."""
coffees = {
"espresso": {"water": 50, "milk": 0, "coffee": 18, "price": 1.5},
"latte": {"water": 200, "milk": 150, "coffee": 24, "price": 2.5},
"cappuccino": {"water": 250, "milk": 100, "coffee": 24, "price": 3.0}
}
stock = {"water": 1000, "milk": 500, "coffee": 100, "money": 0}
return coffees, stock
This function defines and returns the coffee options and stock.
Step 2: Function to Collect Coffee Selection from the User
In the second step, the coffee selection will be collected from the user.
def get_user_selection():
"""Collects coffee selection from the user."""
selection = input("Which coffee would you like? (espresso/latte/cappuccino): ").lower()
return selection
This function collects and returns the coffee selection from the user.
Step 3: Function to Check the Necessary Ingredients and Prepare the Coffee Based on the Selected Type
In the third step, the necessary ingredients will be checked, and the coffee will be prepared based on the selected type.
def prepare_coffee(coffees, stock, selection):
"""Checks the necessary ingredients and prepares the coffee based on the selected type."""
if selection in coffees:
enough_ingredients = True
for ingredient in coffees[selection]:
if ingredient != "price" and coffees[selection][ingredient] > stock[ingredient]:
print(f"Not enough {ingredient}.")
enough_ingredients = False
break
if enough_ingredients:
for ingredient in coffees[selection]:
if ingredient != "price":
stock[ingredient] -= coffees[selection][ingredient]
stock["money"] += coffees[selection]["price"]
print(f"{selection} is ready! Enjoy.")
return enough_ingredients
else:
print("Invalid selection.")
return False
This function checks the necessary ingredients, prepares the coffee if enough ingredients are available, and updates the stock.
Step 4: Function to Print the Result
In the fourth step, the result will be printed.
def print_result(stock):
"""Prints the updated stock."""
print("Remaining stock:")
for ingredient, amount in stock.items():
print(f"{ingredient}: {amount}")
This function prints the updated stock.
Step 5: Function to Start the Coffee Machine Application
This function starts the Coffee Machine application and manages its flow.
def start_coffee_machine():
"""Starts the Coffee Machine application."""
coffees, stock = define_coffee_options_and_stock()
selection = get_user_selection()
prepare_coffee(coffees, stock, selection)
print_result(stock)
# Start the application
start_coffee_machine()
This function starts the Coffee Machine application and manages its flow. It collects the necessary data from the user, prepares the coffee, and prints the updated stock.
Concepts Used
Data Types: In this project, dictionaries (coffee options, stock) and strings (user input) are used.
Conditional Statements: if-else structures are used to check the coffee selection and perform the correct operation.
Functions: Functions make the code modular and reusable.
Loops: A for loop is used to check and update the ingredients.