Saturday, January 23, 2021

Python - Setup local environment (Day 15)

This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp 

I will post some notes to motivate myself to finish this challenge.


Install Python





Install PyCharm (Option 1)



* Spell check
* More Space (View two files side by side)
* Built-In Linter
* Local History
* View Structure
* Refactor Rename


Install VS Code and its extension (Option 2)



* Formatting: Formatting makes code easier to read by human beings by applying specific rules and conventions for line spacing, indents, spacing around operators, and so on
    $ pip install autopep8

* Linting: Linting highlights syntactical and stylistic problems in your Python source code
     $ pip install pylint


Challenge - Coffee Machine



Build a Coffee Machine Program which support some functionalities below:
    * Prompt user by asking “What would you like? (espresso/latte/cappuccino):
    * Turn off the Coffee Machine by entering “off” to the prompt
    * Print report.
    * Check resources sufficient?
    * Process coins
    * Check transaction successful?
    * Make Coffee.

Ex: 
resource.py
MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

main.py
from resource import MENU

# Constants
QUARTER = 0.25
DIME = 0.1
NICKEL = 0.05
PENNY = 0.01


def print_report(resource, profit):
    """Print current resource and profit"""
    # Resource
    for key in resource:
        if key == "Coffee":
            print(f"{key}: {resource[key]}g")
        else:
            print(f"{key}: {resource[key]}ml")

    # Profit
    print(f"Money: ${profit}")


def is_sufficient_resource(coffee_item_ingredients, resource):
    """Return True/False to determine whether the resouce is
enough to make a specific coffee"""
    if "water" in coffee_item_ingredients:
        if coffee_item_ingredients["water"] > resource["Water"]:
            print(f"Sorry there is not enough water")

            return False

    if "milk" in coffee_item_ingredients:
        if coffee_item_ingredients["milk"] > resource["Milk"]:
            print(f"Sorry there is not enough milk")

            return False

    if "coffee" in coffee_item_ingredients:
        if coffee_item_ingredients["coffee"] > resource["Coffee"]:
            print(f"Sorry there is not enough coffee")

            return False

    return True


def collect_coins():
    """Ask user to input and return the total value of coins"""
    print("Please insert coins.")
    q = int(input("how many quarters?:"))
    d = int(input("how many dimes?:"))
    n = int(input("how many nickles?:"))
    p = int(input("how many pennies?:"))

    return q * QUARTER + d * DIME + n * NICKEL + p * PENNY


def is_enough_money(money, item):
    """Return True/False to determine whether it is enough money to
make a specific coffee"""
    change = money - item["cost"]

    if change >= 0:
        print(f"Here is ${change} in change.")

        return True
    else:
        print("Sorry that's not enough money. Money refunded.")

        return False


def make_coffee(user_select, coffee_item_ingredients, resource):

    if "water" in coffee_item_ingredients:
        resource["Water"] = resource["Water"] -
coffee_item_ingredients["water"]
    if "milk" in coffee_item_ingredients:
        resource["Milk"] = resource["Milk"] -
coffee_item_ingredients["milk"]
    if "coffee" in coffee_item_ingredients:
        resource["Coffee"] = resource["Coffee"] -
coffee_item_ingredients["coffee"]

    print(f"Here is your {user_select} ☕️. Enjoy!")

    return resource


def coffee_machine():
    profit = 0
    resource = {
        "Water": 300,
        "Milk": 200,
        "Coffee": 100,
    }

    is_machine_on = True

    while is_machine_on:

        user_select = input("What would you like?
(espresso/latte/cappuccino):")
        if user_select == "report":
            print_report(resource, profit)
        elif user_select == "off":
            is_machine_on = False
        else:
            coffee_item = MENU[user_select]

            # Enough resouce?
            if is_sufficient_resource(coffee_item["ingredients"],
resource):
                money = collect_coins()

                # Enough money?
                if is_enough_money(money, coffee_item):
                    # Update profit
                    profit += coffee_item["cost"]

                    # Make coffee and Update resource
                    resource = make_coffee(
                        user_select, coffee_item["ingredients"],
resource
                    )


coffee_machine()


No comments:

Post a Comment