Final answer:
To create a program in Python for purchasing items, you can follow steps such as creating a dictionary for item names and prices, prompting the user for quantities and shipping preferences, calculating totals, and displaying the results.
Step-by-step explanation:
Python Program for Purchasing Items
To write a program in Python for purchasing items, you can follow these steps:
- Create a dictionary to store the item names and prices.
- Prompt the user to enter the quantity of each item they want to purchase and whether they want in-store pickup or shipping.
- Calculate the total for each item by multiplying the quantity by its price.
- Sum up the totals of all items to get the subtotal.
- Determine the shipping cost based on the user's choice.
- Add the subtotal and shipping cost to get the total due.
- Display the totals for each item, the shipping cost, and the total due.
Here is an example code:
items = {'Item 1': 10, 'Item 2': 20, 'Item 3': 30}
quantities = {}
# Prompt for inputs
for item in items.keys():
quantities[item] = int(input(f'Enter quantity for {item}: '))
shipping_choice = input('Enter your shipping choice (pickup/ship): ')
# Calculate totals
subtotal = 0
total_due = 0
for item, quantity in quantities.items():
total = items[item] * quantity
subtotal += total
print(f'Total for {item}: {total}')
# Calculate shipping cost
if shipping_choice == 'pickup':
shipping_cost = 0
else:
shipping_cost = 5
# Calculate total due
total_due = subtotal + shipping_cost
# Display results
print(f'Subtotal: {subtotal}')
print(f'Shipping Cost: {shipping_cost}')
print(f'Total Due: {total_due}')