Question

In: Computer Science

Python This week you will write a program in Python that mimics an online shopping cart...

Python

This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. 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 and shipping costs and returns their values to the main program

Solutions

Expert Solution

Here is the code :


import random
def store():
    items={"apple":20,"banana":10,"mango":30,"strawberry":40,"lichi":15,"grapes":25,"guava":26}
    return items

def printCart(cart,name,address,tax,shippingCost):  #all details of cart is printed
    total=0.0
    print('----------Your Cart---------')
    for item in cart:
        total=total+item[2]
        print('Item name : {}\nQuantity : {}\nPrice : {}'.format(item[0],item[1],item[2]))
    print('--------')
    print('Name : {}\nAddress : {}\nTax:{}\nShipping Cost : {}'.format(name,address,tax,shippingCost))
    print('\nTotal Price : {}'.format(total+tax+shippingCost)) 

def computeTax(cart):
    tax=0.0;
    for item in cart:
        tax=tax+item[2]*5/100 #5% per item price
    return tax

def genShippingCost(address):
    l=len(address)
    r=random.randint(0,l) #we randomly generate a number by the length of the string address and return a reasonable value as shipping cost
    return r/l*100


def main():
    print("Our Store :")
    print(store()) #The store function defined above contains the item and its price which is called directly inside print function
    print("Enter the item and quantity\nEx:- 'apple 2'\nEnter 'done' to finish\n")
    cart=[]
    while True:
        inp=input()
        if inp=="done": #when user enters done we take the name , address as input and compute tax, shipping cost and finnaly display the cart by calling the print cart function
            print('---------Shipping Details----------\nEnter Name :')
            name=input()
            print('Enter address : ')
            address=input()
            tax=computeTax(cart)
            shippingCost=genShippingCost(address)
            printCart(cart,name,address,tax,shippingCost)
            break #when user enters done and the calculations and printing is over we get out of the loop thus ending the program
        inp=inp.split(' ')
        itemname=inp[0]
        qty=int(inp[1])
        try: #we try fetching the price of the item from the store, if the item is not present in store then an error occurs
            price=store()['{}'.format(itemname)]*qty 
            cart.append([itemname,qty,price])
        except: #if any error occur in the above try block then the below line is executed
            print("The item is not in our store")

main()

Related Solutions

Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. 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...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. 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 -...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from your Program 7). (1) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.(3 pt) ● Parameterized constructor which takes the customer name and date as parameters ● Attributes ○ customer_name (string) ○ current_date (string) ○ cart_items (list) ● Methods ○ add_item() ■ Adds an item to...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the item name and description Private data members string itemDescription -...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT