In: Computer Science
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
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.