Requirements You are required to start with your Lab 5 program - you should have a passing grade (>= 70%) on Lab 5 before starting this assignment other wise you may be building additional function
# Sample Run:
# Welcome to 7Eleven
# Purchase 5 or more items and receive a 10% discount!
# Orders can be delivered to your door for $5.00
# Enter item: cookies
# Enter quantity: 3
# Enter unit price $: 2.0
# Do you want to add more items? (y/n): y
# Enter item: chips
# Enter quantity: 2
# Enter unit price $: 1.5
# Do you want to add more items? (y/n): n
# Do you want this order to be delivered (y/n): y
# Receipt:
TAX_RATE = 0.07
DELIVERY_FEE = 5.00
DISCOUNT_LIMIT = 5
DISCOUNT_PERCENT = 0.10
def print_welcome():
"""Prints a welcome message to the user."""
print("Welcome to 7Eleven")
print(f"Purchase {DISCOUNT_LIMIT} or more items and receive a {DISCOUNT_PERCENT * 100}% discount!")
print(f"Orders can be delivered to your door for ${DELIVERY_FEE:.2f}.")
def get_user_input(prompt, input_type):
"""Prompts the user and returns the input converted to the specified type."""
while True:
try:
return input_type(input(prompt))
except ValueError:
print(f"Invalid input. Please enter a valid {input_type.__name__}.")
def get_item():
"""Prompts for item details and returns them as a list."""
item_name = input("Enter item: ")
quantity = get_user_input("Enter quantity: ", int)
unit_price = get_user_input("Enter unit price $: ", float)
return [item_name, quantity, unit_price]
def add_item_to_cart(cart, item):
"""Adds an item to the cart."""
cart.append(item)
def calculate_subtotal(cart):
"""Calculates the subtotal of all items in the cart."""
subtotal = 0.0
for item in cart:
subtotal += item[1] * item[2]
return subtotal
def apply_discount(total_items, subtotal):
"""Applies a discount if total_items exceed DISCOUNT_LIMIT."""
if total_items >= DISCOUNT_LIMIT:
return subtotal * DISCOUNT_PERCENT
return 0.0
def calculate_total_cost(subtotal, discount, tax, delivery_cost):
"""Calculates the final total cost including tax and delivery fee."""
return subtotal - discount + tax + delivery_cost
def print_receipt(cart, delivery_cost, total_cost):
"""Prints the order receipt """
print("\nReceipt:")
for item in cart:
item_name, quantity, unit_price = item
item_total = quantity * unit_price
print(f"{item_name}: {quantity} @ ${unit_price:.2f} each = ${item_total:.2f}")
if delivery_cost > 0:
print(f"Delivery fee: ${delivery_cost:.2f}")
print(f"Total cost: ${total_cost:.2f}")
print("Thank you for shopping!")
def main():
"""Main function to drive the program."""
print_welcome()
while True:
cart = []
total_items = 0
while True:
item = get_item()
add_item_to_cart(cart, item)
total_items += item[1]
more_items = input("Do you want to add more items? (y/n): ").lower()
if more_items != 'y':
break
subtotal = calculate_subtotal(cart)
discount = apply_discount(total_items, subtotal)
tax = subtotal * TAX_RATE
delivery_cost = DELIVERY_FEE if input("Do you want this order to be delivered (y/n): ").lower() == 'y' else 0
total_cost = calculate_total_cost(subtotal, discount, tax, delivery_cost)
print_receipt(cart, delivery_cost, total_cost)
if input("Do you want to run the program again? (y/n): ").lower() != 'y':
break
if __name__ == "__main__":
main()