Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Code Challenge #8: Shopping Cart
- See README on GITHUB for instructions
- Program Analysis
- Inputs:
- enter food name
- enter food price
- enter quantity
- Outputs:
- Food name, price, and amount
- Add up all the prices together, plus tax
- print all the food items as a grocery list
- Algorithm:
- Enter food name
- Enter food price
- Enter food amount
- add up the other foods on the list
- print the shopping cart list with the Sub-Total, Sales Tax, and Total cost
- '''
- # Write your code here
- '''
- Function analysis:
- Input: food_name , price, food amount
- output:
- adding food and price to the dictionary
- algorithm:
- enter the food name
- enter the price
- enter the amount
- '''
- def add_item(food_list,food_name,food_price,food_amount):
- food_dict = {
- 'name': food_name,
- 'price': food_price,
- 'amount': food_amount
- }
- food_list.append(food_dict)
- return food_list
- '''
- Function Analysis:
- Input: remove_food
- output: item removed from food list
- algorithm:
- input name of food that is already on the list that you wish to remove
- the food is now removed from the list
- '''
- def remove_item(food_list):
- remove_food = input("Please enter the food you would like to remove: ")
- for item in food_list:
- if item['name'] == food_name:
- food_list.remove(item)
- return food_list
- '''
- Function analysis:
- input: food amount, prices, and quantity
- output: list of foods being totaled up
- algorithm:
- for food in the list the price is multiplied by the amount chosen
- and total is totaled up to give the final price
- '''
- def print_list(food_list):
- for item in food_list:
- print("%-30s | $%-7.2f | %-4d | $%-8.2f" % (item['name'], item['price'], item['amount'], item['price'] * item['amount']))
- '''
- Function analysis:
- input: total
- output = shopping_sub_total
- algorithm:
- for item in food_list
- shopping_sub_total equals the item['price'] times item['amount] in the dictionary to recieve a subtotal amount
- '''
- def subtotal(food_list):
- total = 0
- for item in food_list:
- shopping_sub_total = item['price'] * item['amount']
- total = total + shopping_sub_total
- return total
- food_list = []
- while True:
- print("Please choose an item")
- print("1: Add a product")
- print("2: Remove a product")
- print("3: Print the shopping cart")
- print("4: Quit")
- food_input = input("Please choose an option: ")
- if food_input == '1':
- food_name = input("Please enter a food name: ")
- food_price = float(input("What is the price? "))
- food_amount = int(input("How much do you want of this item? "))
- print("{} has been added to the list\n" .format(food_name))
- food_list = add_item(food_list, food_name, food_price, food_amount)
- elif food_input == '2':
- food_list = remove_item(food_list)
- print('Item: {} has been removed\n'.format(remove_item))
- elif food_input == '3':
- print('Your Shopping Cart')
- print('_'*57)
- print('%30s|%-7s|%-4s|%-8s' % ('Name',' Price', 'Amount', 'Sub-Total'))
- print('-'*57)
- print_list(food_list)
- cart_sub_total = subtotal(food_list)
- salestax = cart_sub_total *.08
- cart_total = salestax + cart_sub_total
- print('-'*57)
- print('%45s | $%-8.2f'% ('Sub-Total', cart_sub_total))
- print('%45s | $%-8.2f'% ('Sales Tax (8%)', salestax))
- print('%45s | $%-8.2f'% ('Total', cart_total))
- elif food_input == '4':
- print('Program exited. ')
- break
- elif food_input == None:
- print('None')
- else:
- print('Please use a valid input!')
- '''
- #Questions:
- What are the datatypes needed for the product dictionary?
- I use .remove to remove specific values from the dictionary that were added
- I use .append to make food_list add more items to food_dict dictionary list
- 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.
- 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?
- 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
- Needed the add_item function to create my dictionary list so that I can use it throughout the rest of the program.
- I use the remove function to be able to remove specific items from the list if I wanted to.
- 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.
- 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
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement