Advertisement
Guest User

Untitled

a guest
May 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. # Program: Lesson 6 Assignment
  2. # Programmer: Andrew McKinley
  3. # Date: 5/28/16
  4. # Purpose: The purpose of this program is to demonstrate the use of functions
  5. # To check for bad data and give a friendly error message.
  6.  
  7.  
  8.  
  9. # Define variables
  10. import locale
  11. locale.setlocale( locale.LC_ALL, '')
  12.  
  13. item_cnt = 0 # Amount of items
  14. price = 3.5 # $3.50 for a box
  15. total_qty = 0 # Total amount of boxes
  16.  
  17. cookie_list = []
  18. cookie_list = "Savannah Thin-Mints Tag-a-Longs Peanut-Butter Sandwich".split()
  19.  
  20. order_list = []
  21.  
  22.  
  23.  
  24. # Banner
  25. print("Welcome to your order for girl scout cookies!")
  26. print()
  27.  
  28.  
  29.  
  30. # Function to calculate the total
  31. def calc_tot(qty):
  32. return qty * 3.5
  33.  
  34.  
  35.  
  36. # Function to display the main menu
  37. def disp_menu():
  38. choice_list = ["a", "d", "m", "q"]
  39. while True:
  40. print("What would you like to do?")
  41. print("a = add an item")
  42. print("d = delete an item")
  43. print("m = display the order")
  44. print("q = quit")
  45. choice = input("Make a choice > ")
  46. if choice in choice_list:
  47. return choice
  48. else:
  49. print("Invalid option. Please try again.")
  50.  
  51.  
  52.  
  53. # Function to display the items in the cookie list
  54. def disp_items():
  55. print("Please choose a cookie flavor. Enter the item number.")
  56. for c in range(len(cookie_list)):
  57. print("{}.\t{}".format(c+1, cookie_list[c]))
  58. print()
  59.  
  60.  
  61.  
  62. # Function for the add process
  63. def add_process():
  64. # Get and validate the user's input
  65. valid_data = False
  66. while not valid_data:
  67. disp_items()
  68. try:
  69. item = int(input("Enter an item number > "))
  70. if 1 <= item <= len(cookie_list):
  71. valid_data = True
  72. else:
  73. print("\nInvalid item number. Please try again.")
  74.  
  75. except Exception as detail:
  76. print("error:", detail)
  77.  
  78. # Validate the quantity input
  79. valid_data = False
  80. while not valid_data:
  81. try:
  82. qty = int(input("Enter the quantity > "))
  83.  
  84. if 1 <= qty <= 10:
  85. valid_data = True
  86. else:
  87. print("\nThat was not a valid amount. Please try again.")
  88. except Exception as detail:
  89. print("Error:", detail)
  90. print("Please try again.")
  91.  
  92. # Multiplication for the function
  93. item_total = locale.currency(calc_tot(qty), grouping=True)
  94.  
  95. # Display the users choice
  96. print("\nYou chose: {} boxes of {} for a total of {}".format(qty,
  97. cookie_list[item-1],
  98. item_total))
  99. print()
  100.  
  101. # Verify before including this item
  102. valid_data = False
  103. while not valid_data:
  104. incl = input("Would you like to include this in your order? (y/n) > ")
  105. print()
  106. if incl.lower() == "y":
  107. inx = item - 1
  108. detail_list = [inx, qty]
  109. order_list.append(detail_list)
  110. valid_data = True
  111. print("{} was added to your order.".format(cookie_list[inx]))
  112. elif incl.lower() == "n":
  113. print("{} was not added to your order.".format(cookie_list[inx]))
  114. valid_data = True
  115. else:
  116. print("Invalid response. Please try again.")
  117.  
  118.  
  119.  
  120. # Function used to delete an item from the list
  121. def del_item():
  122. if len(order_list) == 0:
  123. print("You have no items in your order to delete.")
  124. else:
  125. print("\nChoose an item to delete")
  126. disp_order()
  127. valid_data = False
  128. while not valid_data:
  129. try:
  130. choice = int(input("Enter the item number you would like to delete > "))
  131. if 1 <= choice <= len(order_list):
  132. choice = choice - 1
  133. print("Item #{}. {} with {} boxes has been deleted.".format(choice + 1,
  134. order_list[choice][0],
  135. order_list[choice][1]))
  136. del order_list[choice]
  137. del order_list[choice]
  138. valid_data = True
  139. except Exception as detail:
  140. print("Error:", detail)
  141. print("Please try again.")
  142.  
  143.  
  144.  
  145. # Function to display the order
  146. def disp_order():
  147. order_total = 0.0
  148. print()
  149. print("Girl scout cookie order for NAME")
  150. print()
  151. print("Item #\tName\t\tQuantity\tPrice")
  152. for c in range (len(order_list)):
  153. item_total = calc_tot(order_list[c][1])
  154. fmt_item_total = locale.currency(item_total, grouping=True)
  155. order_total += item_total
  156. fmt_order_total = locale.currency(order_total, grouping=True)
  157. print("{}.\t\t{}\t\t\t{}\t\t\t{}".format(c + 1,
  158. order_list[c][0],
  159. order_list[c][1],
  160. fmt_item_total))
  161. print("-" * 20)
  162. print("Your order contains {} amount of items for a total of {}.".format(len(order_list),
  163. fmt_order_total))
  164.  
  165. # Main Program
  166. while True:
  167. choice = disp_menu()
  168. if choice == "a":
  169. add_process()
  170. elif choice == "d":
  171. del_item()
  172. elif choice == "q":
  173. break
  174. disp_order()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement