使用Python打印小票,你可以按照以下步骤进行:
导入必要的库
```python
import os
import sys
from datetime import datetime
from decimal import Decimal
```
设置全局变量
```python
RECEIPT_NO = os.environ.get("RECEIPT_NO") or "N/A"
RECEIPT_DATE = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
TAX_RATE = Decimal(0.08)
STORE_NAME = "Your Store Name"
STORE_ADDRESS = "Your Store Address"
```
创建项目列表
```python
items = [
{"name": "Product 1", "quantity": 1, "price": Decimal(10.00), "total": Decimal(10.00)},
{"name": "Product 2", "quantity": 2, "price": Decimal(5.00), "total": Decimal(10.00)},
]
```
计算总计
```python
subtotal = sum(item["total"] for item in items)
tax = subtotal * TAX_RATE
total = subtotal + tax
```
定义打印函数
```python
def print_receipt(receipt_no, receipt_date, store_name, store_address, items, subtotal, tax, total):
print(f"Receipt No: {receipt_no}")
print(f"Date: {receipt_date}")
print(f"Store Name: {store_name}")
print(f"Store Address: {store_address}")
print(f"Subtotal: {subtotal:.2f}")
print(f"Tax: {tax:.2f}")
print(f"Total: {total:.2f}")
print("\nItems Purchased:")
for item in items:
print(f"{item['name']}: {item['quantity']} x {item['price']:.2f} = {item['total']:.2f}")
print("\nThank you for shopping at our store!")
```
调用打印函数
```python
print_receipt(RECEIPT_NO, RECEIPT_DATE, STORE_NAME, STORE_ADDRESS, items, subtotal, tax, total)
```
这个示例代码展示了如何使用Python创建一个简单的小票打印程序。你可以根据自己的需求进一步扩展和定制这个程序,例如添加更多的商品、税率、折扣等。