【每周python练习-1】简易购物系统
前言
主要用来练习 python 基础类型和条件判断。
代码部分
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022-07-29 10:37
# @Author : hukey
# @Software : PyCharm
good_list = [
{'name': '苹果', 'price': 10},
{'name': '香蕉', 'price': 20},
{'name': '菠萝', 'price': 30},
]
shopping_car = {}
money = input('输入金额:').strip()
if money.isdigit() and int(money) > 0:
while True:
for index, val in enumerate(good_list):
print('{}\t商品名:{}\t单价:{}'.format(index+1, val['name'], val['price']))
choice = input('输入序号【e:查询余额 q:退出】:').strip()
if choice.isdigit() and int(choice) <= len(good_list):
num = input('请输入数量:').strip()
if num.isdigit():
if int(money) >= good_list[int(choice)-1]['price'] * int(num):
money = int(money) - good_list[int(choice)-1]['price'] * int(num)
if good_list[int(choice)-1]['name'] in shopping_car:
shopping_car[good_list[int(choice)-1]['name']] = shopping_car[good_list[int(choice)-1]['name']] + int(num)
else:
shopping_car[good_list[int(choice)-1]['name']] = int(num)
print('购买成功,余额:%.2f 元' % money)
print('购物车'.center(20,'#'))
for k, v in shopping_car.items():
print('商品名:{}\t数量:{}'.format(k, v))
print(''.center(22, '#'))
else:
print('\033[31;1m购买失败。\033[0m')
else:
print('\033[31;1m请输入正确数量\033[0m')
elif choice.lower() == 'q':
print('欢迎下次光临!')
break
elif choice.lower() == 'e':
print('余额为:%.2f 元' % float(money))
else:
print('\033[31;1m请输入正确的序号。\033[0m')
else:
print('\033[31;1m请输入正确的金额.\033[0m')
本文作者:hukey
本文链接:https://www.cnblogs.com/hukey/p/16531580.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步