python超市收银统计和找零程序
时间: 2023-12-16 10:04:23 浏览: 184
好的,我来为您介绍一下Python超市收银统计和找零程序。
1. 超市收银统计程序
超市收银统计程序可以统计每个顾客的购买信息,包括商品名称、数量、单价、总价等,并且可以计算所有顾客的购买总金额和商品销售排行榜等。
以下是一个超市收银统计程序的代码示例:
```
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
self.total_quantity = 0
self.total_sales = 0
def add(self, quantity):
self.total_quantity += quantity
self.total_sales += quantity * self.price
def get_sales(self):
return self.total_sales
def print_ranking(self, rank):
print("第%s名:%s,销售额:%s" % (rank, self.name, self.total_sales))
class Customer:
def __init__(self, id):
self.id = id
self.products = {}
self.total_price = 0
def add_product(self, product, quantity):
if product.name in self.products:
self.products[product.name] += quantity
else:
self.products[product.name] = quantity
self.total_price += product.price * quantity
def print_receipt(self):
print("顾客%s的购物清单:" % self.id)
for name, quantity in self.products.items():
price = products[name].price
total = price * quantity
print("%s:%s * %s = %s" % (name, price, quantity, total))
print("总价:%s" % self.total_price)
products = {
"牛奶": Product("牛奶", 5.0),
"面包": Product("面包", 3.0),
"饼干": Product("饼干", 2.0)
}
customers = []
for i in range(3):
customer = Customer(i + 1)
for name, product in products.items():
product.add(i + 1)
customer.add_product(product, i + 1)
customers.append(customer)
total_sales = 0
for name, product in products.items():
total_sales += product.get_sales()
sorted_products = sorted(products.values(), key=lambda x: x.get_sales(), reverse=True)
print("商品销售排行榜:")
for i, product in enumerate(sorted_products):
product.print_ranking(i + 1)
print("总销售额:%s" % total_sales)
for customer in customers:
customer.print_receipt()
```
2. 超市找零程序
超市找零程序可以根据顾客的支付金额和商品的总价计算出应找的零钱,并且可以通过尽量减少硬币数量的方式来给出找零结果。
以下是一个超市找零程序的代码示例:
```
def calculate_change(total_price, paid):
change = paid - total_price
coins = [1, 2, 5, 10, 20, 50, 100]
coins_count = {coin: 0 for coin in coins}
while change > 0:
for coin in reversed(coins):
if change >= coin:
coins_count[coin] += 1
change -= coin
break
return coins_count
total_price = 15.5
paid = 20.0
change = calculate_change(total_price, paid)
print("找零:")
for coin, count in change.items():
if count > 0:
print("%s元硬币:%s个" % (coin, count))
```
以上是Python超市收银统计和找零程序的代码示例,希望对您有所帮助。
阅读全文