Statement
Solution
Python Exercise 1.1: The Receipt
Create a Python program that generates a formatted receipt based on user input.
Your program should:
- Ask the user for the name of the item (string).
- Ask for the price of the item (float).
- Ask for the quantity (integer).
- Calculate the subtotal (Price × Quantity).
- Calculate tax (5% of the subtotal).
- Print a formatted receipt showing all details and the Total Cost.
Sample Interaction:
Input
Output
Apple
1.50
10
Enter item name: Apple
Enter price: 1.50
Enter quantity: 10
--------------------
Item: Apple
Subtotal: $15.0
Tax (5%): $0.75
--------------------
TOTAL: $15.75
Solution
Here is one way to solve this using standard input and print formatting.
# 1. Get Inputs
item_name = input("Enter item name: ")
price = float(input("Enter price: "))
qty = int(input("Enter quantity: "))
# 2. Calculations
subtotal = price * qty
tax = subtotal * 0.05
total = subtotal + tax
# 3. Print Output
print("-" * 20)
print(f"Item: {item_name}")
print(f"Subtotal: ${subtotal}")
print(f"Tax (5%): ${tax}")
print("-" * 20)
print(f"TOTAL: ${total}")
Key Concepts:
float()is used for the price because money usually has decimals.int()is used for quantity because you can't buy half an apple usually.f"..."strings are used to insert the variables directly into the text.
Test Console
Run code to see output...