功能:
自定义工资水平,可选商品加购
余额实时提醒
用到的知识点:
列表、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. 安装scipy失败提示lapack not found

    从python库网站下载numpy+mkl合集包通过pip安装在下载scipy安装包通过pip安装即可

  2. 【t085】Sramoc问题

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] Sramoc(K,M)表示用数字0,1,2,...,K-1组成的自然数中能被M整除的最小数.给定K,M ...

  3. Python--day38--多进程的方法属性总结

    多进程的方法属性:

  4. TOJ5705动态序列操作(STL or treap)

    传送门:动态序列操作 在一个动态变化的序列中,完成以下基本操作: (1)插入一个整数 (2)删除一个整数 (3)查找序列中最大的数 (4)查找序列中最小的数 (5)求x的前驱(前驱定义为不大于x的序列 ...

  5. c#中索引器

    https://zhidao.baidu.com/question/59675980.html 不是必要的..相当于数学中的一个函数

  6. vue中的computed和watch区别

    在vue.js官方文档中看到computed和watch获取全名的一个例子: var var vm = new Vue({ el: '#demo', data: { firstName: 'Foo', ...

  7. Qt中动态链接库的使用

    转自: http://www.qtcn.org/bbs/read.php?tid=14719 现在有些软件有自动升级功能,有些就是下载新的DLL文件,替换原来的动态链接库.MFC好象也有类似机制 Qt ...

  8. C语言中的符号总结

    1.注释符号                     //和/* ...*/ 2.续行符号                      \ 3.转义符号                     常用:\ ...

  9. CentOS 7防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭firewall: systemctl stop firewalld.service #停止f ...

  10. Java面向对象程序设计第14章3-8和第15章6

    Java面向对象程序设计第14章3-8和第15章6 3.完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转为字符串. import java.io.*; public class tes ...