用Python编写一个简单的超市收银系统,功能包括收银员登录、商品编号输入、数量输入、总价计算、打折(例如第2件9折、每月的8号、18号、28号商品8.5折)等。附带流程图
时间: 2023-07-04 21:15:18 浏览: 68
很高兴为您解答这个问题!以下是一个简单的超市收银系统的Python代码和流程图:
```python
# 超市收银系统
# 商品信息
products = {
'001': {'name': '可乐', 'price': 3},
'002': {'name': '薯片', 'price': 5},
'003': {'name': '巧克力', 'price': 8},
'004': {'name': '牛奶', 'price': 10},
}
# 打折信息
discounts = {
'2': 0.9, # 第2件9折
'8': 0.85, # 每月8号商品8.5折
'18': 0.85,
'28': 0.85,
}
# 获取打折信息
def get_discount():
day = input('请输入今天的日期:')
if day == '8' or day == '18' or day == '28':
return discounts[day]
else:
return 1
# 登录
def login():
username = input('请输入用户名:')
password = input('请输入密码:')
if username == 'admin' and password == '123456':
print('登录成功!')
return True
else:
print('用户名或密码错误!')
return False
# 输入商品编号和数量
def input_product():
while True:
code = input('请输入商品编号:')
if code in products:
break
else:
print('商品编号不存在,请重新输入!')
while True:
count = input('请输入商品数量:')
if count.isdigit():
break
else:
print('商品数量必须为数字,请重新输入!')
return code, int(count)
# 计算总价
def calculate_price(code, count):
price = products[code]['price'] * count
discount = get_discount()
if str(count) in discounts:
discount *= discounts[str(count)]
return price * discount
# 主函数
def main():
if not login():
return
total_price = 0
while True:
code, count = input_product()
price = calculate_price(code, count)
total_price += price
print('商品名称:{},单价:{},数量:{},总价:{}'.format(products[code]['name'], products[code]['price'], count, price))
is_continue = input('是否继续购买?(y/n)')
if is_continue.lower() != 'y':
break
print('总价:{}'.format(total_price))
if __name__ == '__main__':
main()
```
流程图如下:
![超市收银系统流程图](https://img-blog.csdnimg.cn/img_convert/55b9e9c8c6d8e9e6aaf9ca9b2d8e7e05.png)