Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. '''
  2. Code Challenge #8: Shopping Cart
  3.  
  4. See README on GITHUB for instructions
  5.  
  6. Program Analysis
  7.  
  8. Inputs:
  9. enter food name
  10. enter food price
  11. enter quantity
  12.  
  13.  
  14. Outputs:
  15. Food name, price, and amount
  16. Add up all the prices together, plus tax
  17. print all the food items as a grocery list
  18. Algorithm:
  19. Enter food name
  20. Enter food price
  21. Enter food amount
  22. add up the other foods on the list
  23. print the shopping cart list with the Sub-Total, Sales Tax, and Total cost
  24.  
  25. '''
  26.  
  27.  
  28. # Write your code here
  29. '''
  30. Function analysis:
  31. Input: food_name , price, food amount
  32. output:
  33. adding food and price to the dictionary
  34. algorithm:
  35. enter the food name
  36. enter the price
  37. enter the amount
  38.  
  39.  
  40. '''
  41. def add_item(food_list,food_name,food_price,food_amount):
  42.     food_dict = {
  43.         'name': food_name,
  44.         'price': food_price,
  45.         'amount': food_amount
  46.     }
  47.     food_list.append(food_dict)
  48.     return food_list
  49.  
  50. '''
  51. Function Analysis:
  52. Input: remove_food
  53. output: item removed from food list
  54. algorithm:
  55. input name of food that is already on the list that you wish to remove
  56. the food is now removed from the list
  57.  
  58. '''
  59.  
  60. def remove_item(food_list):
  61.     remove_food = input("Please enter the food you would like to remove: ")
  62.     for item in food_list:
  63.         if item['name'] == food_name:
  64.             food_list.remove(item)
  65.             return food_list
  66. '''
  67. Function analysis:
  68. input: food amount, prices, and quantity
  69. output: list of foods being totaled up
  70. algorithm:
  71. for food in the list the price is multiplied by the amount chosen
  72. and total is totaled up to give the final price
  73. '''
  74. def print_list(food_list):
  75.     for item in food_list:
  76.         print("%-30s | $%-7.2f | %-4d | $%-8.2f" % (item['name'], item['price'], item['amount'], item['price'] * item['amount']))
  77. '''
  78. Function analysis:
  79. input: total
  80. output = shopping_sub_total
  81. algorithm:
  82. for item in food_list
  83. shopping_sub_total equals the item['price'] times item['amount] in the dictionary to recieve a subtotal amount
  84.  
  85. '''
  86. def subtotal(food_list):
  87.     total = 0
  88.     for item in food_list:
  89.         shopping_sub_total = item['price'] * item['amount']
  90.         total = total + shopping_sub_total
  91.     return total
  92.  
  93.  
  94.  
  95. food_list = []
  96.  
  97.  
  98.  
  99.  
  100.  
  101. while True:
  102.     print("Please choose an item")
  103.     print("1: Add a product")
  104.     print("2: Remove a product")
  105.     print("3: Print the shopping cart")
  106.     print("4: Quit")  
  107.    
  108.     food_input = input("Please choose an option: ")
  109.     if food_input == '1':
  110.         food_name = input("Please enter a food name: ")
  111.         food_price = float(input("What is the price? "))
  112.         food_amount = int(input("How much do you want of this item? "))
  113.         print("{} has been added to the list\n" .format(food_name))
  114.         food_list = add_item(food_list, food_name, food_price, food_amount)
  115.     elif food_input == '2':
  116.         food_list = remove_item(food_list)
  117.         print('Item: {} has been removed\n'.format(remove_item))
  118.     elif food_input == '3':
  119.         print('Your Shopping Cart')
  120.         print('_'*57)
  121.         print('%30s|%-7s|%-4s|%-8s' % ('Name',' Price', 'Amount', 'Sub-Total'))
  122.         print('-'*57)
  123.         print_list(food_list)
  124.         cart_sub_total = subtotal(food_list)
  125.         salestax = cart_sub_total *.08
  126.         cart_total = salestax + cart_sub_total
  127.  
  128.         print('-'*57)
  129.         print('%45s | $%-8.2f'% ('Sub-Total', cart_sub_total))
  130.         print('%45s | $%-8.2f'% ('Sales Tax (8%)', salestax))
  131.         print('%45s | $%-8.2f'% ('Total', cart_total))
  132.     elif food_input == '4':
  133.         print('Program exited. ')
  134.         break
  135.     elif food_input == None:
  136.         print('None')
  137.        
  138.     else:
  139.         print('Please use a valid input!')
  140.  
  141.    
  142.  
  143. '''
  144. #Questions:
  145.  
  146. What are the datatypes needed for the product dictionary?
  147. I use .remove to remove specific values from the dictionary that were added
  148. I use .append to make food_list add more items to food_dict dictionary list
  149.  
  150. I use a list in brackets to access the food_dict dictionary, by writing item['name'] , item['amount'] and item['price'], to be able to seek access to the dictionary list and be able to print out the values within these specific keys. Also, to be able to use them to do some simple math to simplify my code within certain functions such as the subtotal function and print_list function.
  151.  
  152. How many function did you use in your program, your function analysis should tell me what they are for. Why did you decide on the function you chose?
  153. I used 4 in total, add_item to add an item to the list, remove_food to remove the item. print_list to print the shopping list. subtotal to recieve the total, subtotal and salestax from the shopping cart
  154. Needed the add_item function to create my dictionary list so that I can use it throughout the rest of the program.
  155. I use the remove function to be able to remove specific items from the list if I wanted to.
  156. I use the print_list function to be able to print out my shopping cart, by printing the list of items with the specific key from my dictionary in brackets within each item.
  157. I use subtotal to be able to print the salestax, total amount, and subtotal from the shopping cart, by doing so i can call the function in my code and print them individual with different purposes within my while loop.abs
  158.  
  159. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement