Question

In: Computer Science

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 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

import random

# Defines a function to return true if parameter quantity is integer
# Otherwise returns false
def validQuantity(qty):
# Initially empty
value = None

# try block begins
try:
  
# Converts the parameter bsr to integer
value = int(qty)
# Handles the exception for string parameter
except ValueError:
print(" ERROR: Quantity cannot be string.")
return False
# Otherwise valid quantity return true
else:
return True

# Defines function to return valid quantity
def getQuantity():
# Loops till valid quantity entered by the user
while(True):
# Accepts quantity
qty = input(" Enter quantit: ")
  
# Calls the function to validate quantity
# If the return result is True
if validQuantity(qty) == True:
# Converts the entered data to integer
qty = int(qty)

# Checks if entered quantity is less than 0
# displays error message
if qty < 0:
print(" Warning! Quantity cannot negative: ", qty)

# Otherwise valid quantity. Returs the quantity
else:
return qty

# Defines function to return price
def getPrice():
# Generates a random index number between 0 and length of the price
# list minus one
# Returns the index position value of price list
return price[random.randrange(0, len(price)-1, 1)]

# Defines function to calculate tax 20%
def calculateTax(amount):
return amount * 0.20

# Creates a price list
price = [12.5, 6.5, 9.7, 22.4, 7.9, 5.6, 8.1, 10.2, 22.4, 9.1]

# Declares a list to store cart information
listCart = []
totalAmount = 0

# Loops till user choice is not "done"
while (True):
# Calls the function to get quantity
qty = getQuantity()
# Calls the function to get price
pr = getPrice()
# Displays the price
print(" Price: $", pr)
# Calculates total amount
totalAmount += qty * pr
# Adds the quantity and price at the end of the list
listCart.append(qty)
listCart.append(pr)

# Accepts user chocie
choice = input(" Would you like to purchase another item (done) to stop: ")
# Converts to lower case
choice = choice.lower()

# Checks if choice is "done" then come out of the loop
if choice == "done":
break

# Calls the function to calculate tax
tax = calculateTax(totalAmount)

# Accepts user name and address
userName = input(" Enter user name: ")
address = input(" Enter shipping address: ")

print(" ", userName, " you have purchased the following items.")
# Loops till end of the list
for c in range(0, len(listCart), 2):
# Displays quantity and price
print("\n Quantity: ", listCart[c], "\n Price: $", listCart[c + 1])

# Displays total amount, tax and amount to pay including tax
print("\n Total Amount: $", totalAmount)
print("\n Tax Amount: $", tax)
print("\n Amount to pay: $", totalAmount + tax)

print(" ", userName, " you products will reach you soon.")

Sample Output:

Enter quantit: ten
ERROR: Quantity cannot be string.
Enter quantit: -10
Warning! Quantity cannot negative: -10
Enter quantit: 10
Price: $ 8.1
Would you like to purchase another item (done) to stop: yes
Enter quantit: 5
Price: $ 9.7
Would you like to purchase another item (done) to stop: yes
Enter quantit: -8
Warning! Quantity cannot negative: -8
Enter quantit: 8
Price: $ 22.4
Would you like to purchase another item (done) to stop: done
Enter user name: Pyari
Enter shipping address: BBSR
Pyari you have purchased the following items.

Quantity: 10
Price: $ 8.1

Quantity: 5
Price: $ 9.7

Quantity: 8
Price: $ 22.4

Total Amount: $ 308.7

Tax Amount: $ 61.74

Amount to pay: $ 370.44
Pyari you products will reach you soon.


Related Solutions

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...
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