The last thing we will learn about in python is functions. Functions are a great way to organize your program - we use them to write code that is executed only under certain circumstances - for instance when you're using Amazon, any of the following are likely functions within their program:
- Add item to cart, determine arrival date, create a new address, compute taxes, compute shipping charges, charge credit card
Because you can write a task-specific function once, and ensure
it works properly, they allow you to break down and write large
programs.
Programming- Python
You will use all the programming techniques we have learned thus
like- including if statements, loops, lists and functions. I leave
the bulk of design up to you (hint - think A LOT about it before
you start programming!!) but it should include all of the
following:
The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price.
- Ensure the user types in numbers for quantities
- "Hardcode" the price of items into a list and pick from it
randomly for each item
- Get the user's name and shipping address
- Separate functions that compute tax (bonus - different based on
shipping state?) and shipping costs (based on number of items?),
and returns their values to the main program
- Bonus: Set up a list of lists with all the items/prices in your
store and only let users pick from that list
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Below is the code to copy: #CODE STARTS HERE----------------
def add_to_cart(inventory,cart): #Add items to cart from inventory print("ITEMS\t\t\t\tPRICE in $") for item in inventory: #Print items in store print("{0:15}{1:10}".format(item[0],item[1])) print("To add items to cart, Enter item name with quantity separated by space. " "Enter 'Done' to stop") while True: #Loop until user enters done inp = input() #Take input if inp.lower() == "done": break #Exit when user enters Done item = " ".join(inp.split()[:-1]) #Get item name try: quantity = int(inp.split()[-1]) #Get item quantity except: print("Quantity must be an integer") #Check if quantity is an int continue price = 0 for x in inventory: #Find inventory for entered item if x[0] == item.lower(): price = x[1] #Get its price break if price == 0: #IF item not in inventory, print message print("item not in inventory") continue else: #Add item to cart temp_list=[item,quantity,price*quantity] cart.append(temp_list) total = print_cart(cart) #Print cart return cart, total #return values def print_cart(cart): #Prints cart itemms with total value total = 0 print("ITEMS\t\t\t\tQUANTITY\t\t\tPRICE in $") for item in cart: print(item[0],"\t\t\t\t",item[1],"\t\t\t\t\t",item[2]) total+=item[2] print("Total : ",total) return total def tax(total): rate = 10 #Change rate as required. rate is in % return total*rate/100 #return tax def shipping(cart): items= len(cart) #Adds $5 per item for shipping return items*5 def main(): cart = [] total = 0 #Inventory of items in store inventory = [["mobile",100],["laptop",150],["monitor",40], ["router",24],["processor",15], ["graphic card",56],["mouse",10],["keyboard",5]] while True: #Loop until user exists print("\nEnter coice:\n1. Add items to cart\n2. Show cart\n3. Checkout\n" "4. Exit") inp = int(input("=> ")) #CAll respective functions if inp == 1: cart,total = add_to_cart(inventory,cart) elif inp == 2: total = print_cart(cart) elif inp == 3: tax_cost = tax(total) shipping_cost = shipping(cart) total = print_cart(cart) print("Tax: ",tax_cost) print("Shipping: ",shipping_cost) print("Grand total: ", total+tax_cost+shipping_cost) elif inp == 4: print("Thank you for shopping with us!") break main() #CODE ENDS HERE------------------
Get Answers For Free
Most questions answered within 1 hours.