Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Program: Lesson 6 Assignment
- # Programmer: Andrew McKinley
- # Date: 5/28/16
- # Purpose: The purpose of this program is to demonstrate the use of functions
- # To check for bad data and give a friendly error message.
- # Define variables
- import locale
- locale.setlocale( locale.LC_ALL, '')
- item_cnt = 0 # Amount of items
- price = 3.5 # $3.50 for a box
- total_qty = 0 # Total amount of boxes
- cookie_list = []
- cookie_list = "Savannah Thin-Mints Tag-a-Longs Peanut-Butter Sandwich".split()
- order_list = []
- # Banner
- print("Welcome to your order for girl scout cookies!")
- print()
- # Function to calculate the total
- def calc_tot(qty):
- return qty * 3.5
- # Function to display the main menu
- def disp_menu():
- choice_list = ["a", "d", "m", "q"]
- while True:
- print("What would you like to do?")
- print("a = add an item")
- print("d = delete an item")
- print("m = display the order")
- print("q = quit")
- choice = input("Make a choice > ")
- if choice in choice_list:
- return choice
- else:
- print("Invalid option. Please try again.")
- # Function to display the items in the cookie list
- def disp_items():
- print("Please choose a cookie flavor. Enter the item number.")
- for c in range(len(cookie_list)):
- print("{}.\t{}".format(c+1, cookie_list[c]))
- print()
- # Function for the add process
- def add_process():
- # Get and validate the user's input
- valid_data = False
- while not valid_data:
- disp_items()
- try:
- item = int(input("Enter an item number > "))
- if 1 <= item <= len(cookie_list):
- valid_data = True
- else:
- print("\nInvalid item number. Please try again.")
- except Exception as detail:
- print("error:", detail)
- # Validate the quantity input
- valid_data = False
- while not valid_data:
- try:
- qty = int(input("Enter the quantity > "))
- if 1 <= qty <= 10:
- valid_data = True
- else:
- print("\nThat was not a valid amount. Please try again.")
- except Exception as detail:
- print("Error:", detail)
- print("Please try again.")
- # Multiplication for the function
- item_total = locale.currency(calc_tot(qty), grouping=True)
- # Display the users choice
- print("\nYou chose: {} boxes of {} for a total of {}".format(qty,
- cookie_list[item-1],
- item_total))
- print()
- # Verify before including this item
- valid_data = False
- while not valid_data:
- incl = input("Would you like to include this in your order? (y/n) > ")
- print()
- if incl.lower() == "y":
- inx = item - 1
- detail_list = [inx, qty]
- order_list.append(detail_list)
- valid_data = True
- print("{} was added to your order.".format(cookie_list[inx]))
- elif incl.lower() == "n":
- print("{} was not added to your order.".format(cookie_list[inx]))
- valid_data = True
- else:
- print("Invalid response. Please try again.")
- # Function used to delete an item from the list
- def del_item():
- if len(order_list) == 0:
- print("You have no items in your order to delete.")
- else:
- print("\nChoose an item to delete")
- disp_order()
- valid_data = False
- while not valid_data:
- try:
- choice = int(input("Enter the item number you would like to delete > "))
- if 1 <= choice <= len(order_list):
- choice = choice - 1
- print("Item #{}. {} with {} boxes has been deleted.".format(choice + 1,
- order_list[choice][0],
- order_list[choice][1]))
- del order_list[choice]
- del order_list[choice]
- valid_data = True
- except Exception as detail:
- print("Error:", detail)
- print("Please try again.")
- # Function to display the order
- def disp_order():
- order_total = 0.0
- print()
- print("Girl scout cookie order for NAME")
- print()
- print("Item #\tName\t\tQuantity\tPrice")
- for c in range (len(order_list)):
- item_total = calc_tot(order_list[c][1])
- fmt_item_total = locale.currency(item_total, grouping=True)
- order_total += item_total
- fmt_order_total = locale.currency(order_total, grouping=True)
- print("{}.\t\t{}\t\t\t{}\t\t\t{}".format(c + 1,
- order_list[c][0],
- order_list[c][1],
- fmt_item_total))
- print("-" * 20)
- print("Your order contains {} amount of items for a total of {}.".format(len(order_list),
- fmt_order_total))
- # Main Program
- while True:
- choice = disp_menu()
- if choice == "a":
- add_process()
- elif choice == "d":
- del_item()
- elif choice == "q":
- break
- disp_order()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement