Skip to content

Instantly share code, notes, and snippets.

@TheFlyingDeutchMan
Last active March 5, 2020 05:17
Show Gist options
  • Save TheFlyingDeutchMan/e49ab254acd7bbfe1085756ffba4401d to your computer and use it in GitHub Desktop.
Save TheFlyingDeutchMan/e49ab254acd7bbfe1085756ffba4401d to your computer and use it in GitHub Desktop.
coffeerun script
#!/usr/bin/env python3
import os
from pprint import pprint
# clear the screen on script start
os.system('cls' if os.name == 'nt' else 'clear')
# SCRIPT EXPLANATION:
#
# This program is to assist me to write down a list of what hot drink they want (Coffee, Hot Chocolate)
# Available hot drinks are 'Flat white, Decaf, Cappucino, Latte, and hot chocolate.
# I need to capture: employee names, type of drink, and number of drinks.
# I need to print a confirmation; capturing order, amount and their names.
# Stretch Goal: we want to display a total number of type of drinks and count of drinks across all employees
# To keep track of all the input I use a list of dictionaries of the following format
#
# an order has the following format:
# [{
# 'employee_name': "Name of employee",
# 'order_items': {
# 'type_of_drink': "name of drink, i.e. latte",
# 'count': 1-3,
# },
# }, ...
# ]
#
# we are storing items available to order in a dictionary
# to have an extensible way to store the menu information
order_menu = {1: "Flat White", 2: "Decaf", 3: "Cappucino", 4: "Hot Chocolate", 5: "Latte"}
# POTENTIAL ISSUES:
#
# 1) You can enter the same employee name multiple times, but it will capture them without overwriting
# 2) You can choose the same drink multiple times for the same person and it will overwrite previous line items
# 3) You can exceed the 3 maxx drink limit
# Todo:
# 1) Have a price for the drinks
# 2) Have total price per person
# 3) Have a overall price
# pprint or 'pretty print' it's useful to print objects pprint(list/dictionary) for debugging
# https://docs.python.org/3/library/pprint.html
# To run this script, install requirements with these commands:
# (pip is a helper program to install py libraries)
# to install in windows use: https://www.liquidweb.com/kb/install-pip-windows/ (if not installed)
# pip install tinydb
# pip install pprint
# COMMENTS RE LINTING:
# a line length of 80 is not a sensible default in 2020 anymore,
# most development teams usually have a default for max line lenght of 120 or 160 even
# pycodestyle --max-line-length=120 --verbose coffeerun.py ; echo $?
# will return 0 (for success)
# also E302 (2 blank spaces between comment and function head) is very commonly ignored
# The purpose of this block of code is to show a banner to show customors where to look
# by defining 'topbanner' it'll make the coding easier
def topbanner(text_1, message, text_2):
print(" "*4, "*"*30)
print(text_1, message, text_2)
print(" "*4, "*"*30)
print()
# using while loop to ask the employee their name
def ask_employee_name():
while True:
name = input("May I have your name, please? ")
if not name:
print("Please enter a valid name :)")
elif name.isalpha():
return name
else:
print("Please use only letters, try again")
# use list comprehension to determine whether an order number is valid
# # https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
def is_valid_order_no(order_no):
return order_no in order_menu
# we want output of the format:
# employee_name - type of drink x number of drinks
# use list comprehension to convert the order_items dictionary to a printable string with the help of join
# we want output of the format:
# employee_name - type of drink x number of drinks
def print_order(current_order):
# print("\nWHOLE ORDER:\n")
# pprint(current_order)
for order_entry in current_order:
# print("\nORDER_ENTRY")
# pprint(order_entry)
print_order_item(order_entry)
def print_order_item(order_entry):
employee_name = order_entry['employee_name']
order_line = ''
for order_entry_item in order_entry['order_items']:
# print("order_entry_item:")
# pprint(order_entry_item)
order_line = order_line + ", " + order_line_item(order_entry_item)
order_message = employee_name + order_line + ", Total (#TODO)"
print(order_message)
def order_line_item(entry):
# print("\nENTRY:\n")
# pprint(entry)
coffee_type = entry['type_of_drink']
count = str(entry['count_of_drinks'])
return (count + ' x ' + coffee_type)
# return ' '.join(['%s x %s, ' % (count, type_of_drink) for (type_of_drink, count) in entry.items()])
def add_line_item(employee_name):
order_msg = '\n'.join(['%s - %s' % (order_no, orderEntry) for (order_no, orderEntry) in order_menu.items()]) + '\n'
print("What type of drink would you like?\n")
while True:
order_no = input(order_msg)
if not order_no:
print("Choose a coffee please\n")
else:
# TODO: rewrite to not rely on using try / except
try:
order_no_int = int(order_no)
if is_valid_order_no(order_no_int):
# return order_menu.get(order_no_int)
type_of_drink = order_menu.get(order_no_int)
break
else:
valid_order_numbers = ', '.join(str(key) for key in order_menu.keys())
print("You can only choose: ", valid_order_numbers)
except ValueError:
print("Please enter only numbers\n")
while True:
print("How many", type_of_drink, "would you like to order?")
count_of_drinks = input()
if count_of_drinks in ["1", "2", "3"]:
count_of_drinks = int(count_of_drinks)
break
else:
print("Your only form of input is: 1, 2, and 3. ")
return dict({'count_of_drinks': count_of_drinks, 'type_of_drink': type_of_drink})
def ask_yes_no():
while True:
yes_no = input()
if yes_no in ["y", "Y", "yes"]:
return True
elif yes_no in ["n", "N", "no"]:
return False
else:
print("Please use y/n or Y/N or yes/no")
def place_order(order_entry_for_employee):
while True:
line_item = add_line_item(employee_name)
print("\nGot Coffee Order", line_item)
order_entry_for_employee['order_items'].append(line_item)
# pprint(order_entry_for_employee)
print("Would you like to add another drink for", order_entry_for_employee['employee_name'].capitalize(), " ?")
if ask_yes_no():
print("Please add an item to your order.!\n")
else:
print("Thank you", employee_name, "your total order is:")
# TODO: Show a nicer output
pprint(order_entry_for_employee)
break
return order_entry_for_employee
topbanner("*"*9, "Hot Morning Beverages", "*"*9)
current_order = []
while True:
employee_name = ask_employee_name()
order_entry = dict({'employee_name': employee_name, 'order_items': []})
this_employees_order = place_order(order_entry)
current_order.append(this_employees_order)
print_order(current_order)
print("Would you like to add another employee's order?")
if ask_yes_no():
True
else:
break
print("\nYour order summary is:\n")
print_order(current_order)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment