È 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("")