Coffee Machine

Python, R, e altri linguaggi usati nella BI, per creare visualizzazioni, implementare tecniche di Machine Learning etc.
Rispondi
Avatar utente

Andrea90
Messaggi: 2192 | Topic creati
Iscritto il: dom 28 giu 2020, 19:41
Luogo: Bologna
Ringraziato: 666 volte
Contatta:

Coffee Machine

Messaggio da Andrea90 »

Buonasera a tutti :wave: ,

È giusto tempo di postare un nuovo giochino realizzato con Python (il file in formato .py non è consentito come allegato al momento, per questo non lo allego), vi lascio il codice da copiare e lanciare sul vostro editor preferito

Le istruzioni su come utilizzarlo sono molto semplici, basta selezionare una delle 3 bevande proposte ed inserire il quantitativo corretto di monete.
Quanto vi chiede quale bevanda provate a scrivere "report" per avere il resoconto delle risorse ancora disponibili.

Per interrompere l'esecuzione del codice, quando vi chiede la bevanda, inserite "off".

Codice: Seleziona tutto


menu = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.50,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.50,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.00,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
    "money": 2.50
}

while True:
    usr_selection = input("What would you like? (espresso/latte/cappuccino): ").lower()
    print("")
    # if the user type 'off' then turn off the coffee machine
    if usr_selection == "off":
        print("Turning off, Bye Bye!")
        break
    # if the user type 'report' show the resources
    if usr_selection == "report":
        print(
            " Water: {}\n".format(resources["water"]),
            "Milk: {}\n".format(resources["milk"]),
            "Coffee: {}\n".format(resources["coffee"]),
            "Money: {}".format(resources["money"])
        )
        continue
    # if user write a beverage that does not exist print:
    if usr_selection not in menu.keys():
        print("You have typed {}, this is not available, please try again!".format(usr_selection))
        continue
    # check if the resources are enough
    if usr_selection in menu.keys():
        cnt_err = 0
        sel_amt = round(menu[usr_selection]["cost"], 3)
        print("You have selected {}, it costs {}".format(usr_selection, sel_amt))
        print("")
        for ingredient in menu[usr_selection]["ingredients"]:
            ing_amt = menu[usr_selection]["ingredients"][ingredient]
            machine_amt = resources[ingredient]
            print("You need {} of {}, you have {} of it".format(ing_amt, ingredient, machine_amt))
            print("*" * 80)
            if ing_amt > machine_amt:
                print("You don't have enough {}".format(ingredient))
                cnt_err += 1
                break
    # check the amount of money that the user has inserted
    if cnt_err == 0:
        usr_quarters = int(input("How many quarters (0.25 $): "))
        usr_dimes = int(input("How many dimes (0.10 $): "))
        usr_nickel = int(input("How many nickel (0.05 $): "))
        usr_pennies = int(input("How many pennies (0.01 $): "))
        usr_money = 0.25 * usr_quarters + 0.10 * usr_dimes + 0.05 * usr_nickel + 0.01 * usr_pennies
        print("")
        cnt_err = 0
        if usr_money < sel_amt:
            print("Sorry that's not enough money. Money refunded.")
            cnt_err += 1
        else:
            new_mach_amt = resources["money"] + sel_amt
            resources["money"] = round(new_mach_amt, 3)
            usr_change = round(usr_money - sel_amt, 3)
            print("The beverage costs {}, you have inserted {}, "
                  "Here is {} dollars in change".format(sel_amt, usr_money, usr_change)
                  )
    # Create the beverage as from the recipe
    if cnt_err == 0:
        for ingredient in menu[usr_selection]["ingredients"]:
            ing_amt = menu[usr_selection]["ingredients"][ingredient]
            machine_amt = resources[ingredient]
            resources[ingredient] = machine_amt - ing_amt
        print("")
        print("☕ "*20)
        print("Here is your {}. Enjoy".format(usr_selection))
        print("☕ "*20)
        print("")



Se hai gradito l'aiuto che hai ricevuto considera di contribuire alle spese per il mantenimento del forum facendo una libera DONAZIONE --> Link

Ricordarsi di segnare come "RISOLTE" le discussioni per le quali si è ricevuto un feedback positivo. Per vedere come fare --> Link

Annieclover2
Messaggi: 1 | Topic creati
Iscritto il: sab 2 apr 2022, 12:49

Coffee Machine

Messaggio da Annieclover2 »

Quale editor stai utilizzando? hai codice per altri giochi oltre a questo? Eseguirò questo codice e il caffè sarà preferito: clap: A proposito, grazie.

N.b. Il link che avevi inserito non aveva nulla a che vedere con la BI, pertanto è stato rimosso.
Avatar utente

Autore del topic
Andrea90
Messaggi: 2192 | Topic creati
Iscritto il: dom 28 giu 2020, 19:41
Luogo: Bologna
Ringraziato: 666 volte
Contatta:

Coffee Machine

Messaggio da Andrea90 »

Annieclover2,

Come editor utilizzo PyCharm, ed al momento non ho altri esempi da mostrare.

Andrea
Se hai gradito l'aiuto che hai ricevuto considera di contribuire alle spese per il mantenimento del forum facendo una libera DONAZIONE --> Link

Ricordarsi di segnare come "RISOLTE" le discussioni per le quali si è ricevuto un feedback positivo. Per vedere come fare --> Link
Rispondi