Skip to content

Instantly share code, notes, and snippets.

@solomonwu
Last active March 18, 2021 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solomonwu/32737f4b7049efbd5117171d0dbcd93d to your computer and use it in GitHub Desktop.
Save solomonwu/32737f4b7049efbd5117171d0dbcd93d to your computer and use it in GitHub Desktop.
[Udemy Python][Day15] Coffee Machine Project
"""
Project: [Udemy Python][Day15] Coffee Machine Project
Name: main.py
"""
import os
import time
def clear(): os.system('cls' if os.name == 'nt' else 'clear')
MENU = {"expresso": { "ingredients": [50, 0, 18], "price": 1.5 },
"latte": { "ingredients": [200, 150, 24], "price": 2.5 },
"cappuccino": { "ingredients": [250, 100, 24], "price": 3.0 } }
COINS = { "quarter": 0.25,
"dime": 0.1,
"nickle": 0.05,
"penny": 0.01 }
MENU_LIST = list( MENU.keys() )
COIN_LIST = list( COINS.keys() )
machine_resource = { "water": 300,
"milk": 200,
"coffee": 100 }
resource_name = list( machine_resource.keys() )
OPTION = { "c": "Continue to buy\n",
"r": "resource report\n",
"p": "power off\n" }
OPTION_LIST = list( OPTION.keys() )
money = 0
def order_menu( available_list ):
clear()
while True:
select = int(input( "What would you like to order?\n"
+ menu_str( available_list ) ) )
if select in available_list:
return select
else:
print( f"{select} is invalid input!" )
def check_resource():
available_menu = []
for menu_num in range( 0, len(MENU_LIST) ):
available_menu.append( menu_num )
resource = list( machine_resource.values() )
for menu_num in range( 0, len(MENU_LIST) ):
menu_resource = MENU[MENU_LIST[menu_num]]["ingredients"]
for item in range( 0, len(resource) ):
if resource[item] - menu_resource[item] < 0:
available_menu.remove( menu_num )
break
return available_menu
def display_resource( show_money = False ):
for ingredient in resource_name:
unit = "ml"
if ingredient == "coffee":
unit = "g"
print( f"{ingredient}: {machine_resource[ingredient]}{unit}" )
if show_money:
print( f"money: ${money}" )
def menu_str( available_list ):
str = ""
select_str =""
for i in available_list:
str += f"{i}. {MENU_LIST[i]}\n"
if i == available_list[-1]:
select_str += f"{i}"
else:
select_str += f"{i}/"
str += f"Type ({select_str}): "
return str
def insert_coin_prompt( choice ):
global money
cost = MENU[MENU_LIST[choice]]["price"]
remain = cost
for coin in COIN_LIST:
coin_num = int(input( f"Remain Cost: ${round(remain, 2)}. "
+ f"How many {coin}s do you pay? " ))
remain -= COINS[coin]*coin_num
if remain <= 0:
make_coffee( choice )
money += cost
print( f"Here is your {MENU_LIST[choice]}!"
+ f" and your change: "
+ f"{round(remain*(-1), 2)}" )
break
if remain > 0:
print( "Not enough money! Return all money: "
+ f"${round(cost- remain, 2)}" )
def make_coffee( choice ):
global machine_resource
resource_name = list( machine_resource.keys() )
need_resource = MENU[MENU_LIST[choice]]["ingredients"]
for item in range( 0, len(machine_resource) ):
machine_resource[resource_name[item]] -= need_resource[item]
def show_main_menu( available_list ):
print( "Back to the main menu" )
countdown( 5 )
clear()
if len( available_list ) == 0:
print( "No enough supply for any coffee" )
# available_option is independent from OPTION_LIST
available_option = OPTION_LIST[:]
if len( available_list ) == 0:
available_option.remove( "c" )
action = option_prompt( available_option )
if action in available_option:
if action == 'r':
display_resource( show_money = True )
elif action == 'c':
return True
elif action == 'p':
return False
else:
print( f"{action} is invalid input!" )
return show_main_menu( available_list )
def option_prompt( available ):
str = ""
select_str = ""
for option in available:
str += f"{option}: {OPTION[option]}"
if option == available[-1]:
select_str += f"{option}"
else:
select_str += f"{option}/"
return input( f"{str} Type ({select_str}) ")
# countdown timer
def countdown( t ):
while t: # while t > 0 for clarity
mins = t // 60
secs = t % 60
timer = '{:02d}:{:02d}'.format(mins, secs)
print( timer, end="\r" ) # overwrite previous line
time.sleep( 1 )
t -= 1
def coffee_machine():
running = True
while running:
available_list = check_resource()
if len( available_list ) > 0:
choice = order_menu( available_list )
insert_coin_prompt( choice )
available_list = check_resource()
running = show_main_menu( available_list )
# Main program
coffee_machine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment