功能:
自定义工资水平,可选商品加购
余额实时提醒
用到的知识点:
列表、if多分支、循环、高亮输出
未解决bug
删除商品后不能自动退出
代码如下:
if shopping_list:
shopping_list.pop(user_choice)
print("已删除")
else:
print("你没有购买任何商品")
del_list=0 #此处del_list为循环标志位
break

完整代码如下:

 product_list = [ ('Iphone',5800),
('荣耀Watch',999),
('honorV20',2399),
('Iphone10',10000),]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():#isdigit方法判断字符串是否只由数字组成
salary = int(salary)
print("你可以购买如下商品:")
while True:
for index,item in enumerate(product_list):
# print(product_list.index(item),item)
print(index+1,item)#将序号调整为1234
user_choice = input(
'''选择要买什么?
输入商品序号选择商品、'index'查看购物车、'del'开始删除所选商品、'del all'清空购物车、输入'q'退出!
--->''')
if user_choice.isdigit():
user_choice = int(user_choice)
user_choice-=1#对应减一保证序列对应所选商品
if user_choice <len(product_list) and user_choice>=0:
# 将选择的商品序号记录并进行相应判断处理
p_item = product_list[user_choice]
if p_item[1] <=salary:
shopping_list.append(p_item)#添加购物车清单
salary -= p_item[1]#工资消耗
print(
'''Added %s into shopping cart,余额: \033[31;1m%s\033[0m
'''%(p_item,salary))
else:
print("\033钱好像不够了,还剩[%s]\033[0m" % salary)
else:
print("商品 [%s] 不存在!"% user_choice)
elif user_choice == 'index':
print("已经加购的商品:")
for p in shopping_list:
print(p)
print("-*-*-*-*-*-*-*-*")
print("开始选择商品:")
elif user_choice == 'q':
print("-------shopping list--------")
if len(shopping_list)==0:
print("购物车空空如也!")
for p in shopping_list:
print(p)
print("你的余额为:",salary)
exit()
elif user_choice == 'del':
for index, item in enumerate(shopping_list):
# print(product_list.index(item),item)
print(index + 1, item) # 将序号调整为1234
del_list=1
while del_list:
user_choice=input("删除哪个?")
if user_choice.isdigit():
user_choice = int(user_choice)
user_choice -= 1 # 对应减一保证序列对应所选商品
if user_choice < len(shopping_list) and user_choice >= 0:
if shopping_list:
shopping_list.pop(user_choice)
print("已删除")
else:
print("你没有购买任何商品")
del_list=0
break
elif user_choice == 'del all':
del shopping_list
print("开始重新选择商品")
elif user_choice == 'q':
print("开始选择商品")
del_list=0
else:
print("对不起,没有该选项!")
else:
print("对不起,没有该选项!")

输出结果:

欢迎访问我的博客:cnblogs.com/zhq-home

第五篇:python购物车小程序开发demo的更多相关文章

  1. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  2. 微信小程序开发demo

    自己写的小程序,欢迎下载 https://gitee.com/lijunchengit/chengZiShengHuoBang

  3. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

  4. 第五篇、微信小程序-swiper组件

    常用属性: 效果图: swiper.wxml添加代码: <swiper indicator-dots="{{indicatorDots}}" autoplay="{ ...

  5. 微信小程序开发-入门到熟练(wepy-初级篇)

    Title:最近做完了项目,review代码的同时,就想写一篇详细的小程序开发经历,记录自己的项目从0到1的过程 Desc : 小程序从0到1,从小白到完成项目,你需要这样做: step1: 基础知识 ...

  6. 【微信小程序开发】全局配置

    今天看看小程序全局配置. 上一篇[微信小程序开发]秒懂,架构及框架 配置,无非就是为了增加框架的灵活性,而定下的规则. 微信小程序的配置文件是一个树状结构,各个节点代表不同的配置项,小程序框架会解析这 ...

  7. 【微信】2.微信小程序开发--官方开发工具使用说明

    承接第一篇 =============================================== 关于微信小程序开发使用IDE,曾经自己动摇过. 到底是采用 微信官方小程序开发工具 WebS ...

  8. 微信小程序开发系列二:微信小程序的视图设计

    大家如果跟着我第一篇文章 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 一起动手,那么微信小程序的开发环境一定搭好了.效果就是能把该小程序的体验版以二维码的方式发送给其他朋友使用. 这个系列 ...

  9. 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)

    七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...

随机推荐

  1. Spring Boot笔记之邮件(spring-boot-starter-mail)

    Spring Boot环境中发送邮件 pom.xml引入`spring-boot-starter-mail` application.yml配置 163邮箱 QQ邮箱 Gmail邮箱 发送邮件 ser ...

  2. 【codeforces 761D】Dasha and Very Difficult Problem

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. linux一些重要数据结构

    如同你想象的, 注册设备编号仅仅是驱动代码必须进行的诸多任务中的第一个. 我们将很 快看到其他重要的驱动组件, 但首先需要涉及一个别的. 大部分的基础性的驱动操作包括 3 个重要的内核数据结构, 称为 ...

  4. Java 5,6,7,8,9,10,11新特性吐血总结

    作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892 博客地址:https://it18monkey.github.io 转载请注明出处 java5 ...

  5. 2019-11-17-dotnet-core-使用-GBK-编码

    title author date CreateTime categories dotnet core 使用 GBK 编码 lindexi 2019-11-17 16:36:27 +0800 2019 ...

  6. vue-learning:22 - js - directives

    directives 在讲解视图层指令时,我们讲到ref特性,使用它我们可以获取当前DOM元素对象,以便执行相关操作. <div id="app"> <input ...

  7. 关于instanface的问题

    nstanceof关键字来判断某个对象是否属于某种数据类型.报错  代码如下 package cn.lijun.demo3; import cn.lijun.demo.Person;import cn ...

  8. ES的索引查询和删除

    postman 1.查看es状态 get http://127.0.0.1:9200/_cat/health 红色表示数据不可用,黄色表示数据可用,部分副本没有分配,绿色表示一切正常 2.查看所有索引 ...

  9. python multiprocessing.freeze_support

    Running on windows platform, give me an error as below: File "C:\Python\lib\multiprocessing\for ...

  10. python 处理xml文件

    需求 在实际应用中,需要对xml配置文件进行实时修改, 1.增加.删除 某些节点 2.增加,删除,修改某个节点下的某些属性 3.增加,删除,修改某些节点的文本 <annotation> & ...