4 min read 0 comments

I just finished re-doing one of my assignments for an online course on Python. The pirate bartender app is an app that asks you what kind of drink do you like by asking a number of questions that are part of a set dictionary. The app then creates the drink based on your response. Furthermore, when done with a drink, the app asks you if you want another drink. The app is made more fun because it talks in pirate.

So for this case, two dictionaries were used. One is a questions dictionary that is composed of categories as keys and questions as values. Depending on the answers to the questions, a profile of a drink is created.

questions = {
    "strong": "Do ye like yer drinks strong?",
    "salty": "Do ye like it with a salty tang?",
    "bitter": "Are ye a lubber who likes it bitter?",
    "sweet": "Would ye like a bit of sweetness with yer poison?",
    "fruity": "Are ye one for a fruity finish?",
}

Another dictionary is the ingredients dictionary that is composed of three possible ingredients per drink characteristic. The drink is created by randomly choosing one of these ingredients if the user chooses that particular drink characteristic.

ingredients = {
    "strong": ["glug of rum", "slug of whisky", "splash of gin"],
    "salty": ["olive on a stick", "salt-dusted rim", "rasher of bacon"],
    "bitter": ["shake of bitters", "splash of tonic", "twist of lemon peel"],
    "sweet": ["sugar cube", "spoonful of honey", "spash of cola"],
    "fruity": ["slice of orange", "dash of cassis", "cherry on top"],
}

To create the main program, I first print a header for the app. Next, I call a function that asks for the customer the characteristic he or she wants in the drink. Then, based on the recorded profile, the ingredients dictionary is used to create the drink. The process is done in a loop such that if the user wants another drink, the whole thing repeats and stops if not.

def main():
    # print header
    header()
    # set a condition for the while loop
    repeat = True
    while repeat:
        # ask customer how they like their drink; create another dict capturing customer response
        responses = ask_customer(questions)
        # based on customer liking, create the drink using the ingredients dict
        create_drink(responses, ingredients)
        # change or not change the value of the variable repeat used to make the loop go one more time or not
        repeat = ask_for_another_drink()

The functions used above are then created as follows:

def header():
    print("---------------------------------")
    print("    THE PIRATE BARTENDER APP")
    print("---------------------------------\n")

The header function is just so that the user knows that the app has started to run.

def ask_customer(questions):
    print("What style drink do ye like?")
    print()
    responses = {}
    for key, value in questions.items():
        response = input(value + " (y/n) ")
        if response.lower() == "y" or response.lower() == "yes":
            responses[key] = True
        else:
            responses[key] = False
    return responses

The ask_customer function obtains user response on questions about the drink characteristic. Here, a new dictionary, responses is created. This dictionary takes on the key of the the questions dictionary. The for loop is used to pose the questions, and depending on the answer of the user, the value for the keys in the responses dictionary are set to True or False. In short, the responses dictionary would look something like so:

responses = {
    "strong": True,
    "salty": True,
    "bitter": False,
    "sweet": True,
    "fruity": True
}

The next function, create_drink uses the responses dictionary and the ingredients dictionary.

def create_drink(responses, ingredients):
    drink = []
    for key, response in responses.items():
        if response:
            drink.append(random.choice(ingredients[key]))
    print()
    if drink:
        print("Ere is yer drink:")
        for ingredient in drink:
            if ingredient == drink[-1]:
                print("\tand a " + ingredient.title() + "!!!!")
            else:
                print("\t" + ingredient.title() + "!")
    else:
        print("Well, I don't think ye want a drink, then!")
    print()

A list, drink is created and the ingredients are added to this list depending on whether the characteristic or key in the responses dictionary is true or false. This is done in the first five lines of the function above. After this, the list of ingredients is presented in pirate talk.

Lastly, a function is created to determine if the user or customer wants another drink. Here, recursion, the use of a function within the same function, is taken advantage of if the response to the question is not understood.

def ask_for_another_drink():
    another_drink = input("P'raps another drink? (y/n) ")
    if another_drink.lower() in ['no', 'n']:
        print("Off with ye then!\n")
        return False
    elif another_drink.lower() in ['yes', 'y']:
        print("Aye!\n")
        return True
    else:
        print("Mind repeatin that, matey?\n")
        return ask_for_another_drink() # recursion

An example of a run for this app is as follows:

I had fun making this app. There are still room for improvement, such as in the ask_customer function. If the user response is not understood, there is no way to address that. As it stands right now, if the response is not ‘yes’ or ‘y’, it takes the ‘no’ option.

The source code for this app is on my GitHub.