功能:
自定义工资水平,可选商品加购
余额实时提醒
用到的知识点:
列表、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. jQuery 选择器 bug

    $(function(){ $(".menu li").hide(); //目标对象(一定要用class或id选择器)绑定函数 $(".menu").click ...

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

    多进程的方法属性:

  3. H3C IEEE EUI-64格式

  4. java的四种代码块

    用{}括起来的称为代码块: 普通代码块:类中方法的方法体 构造代码块:类中{}直接括起来的语句,每次创建对象都会被调用,先于构造函数执行 静态代码块:类中static{}括起来的语句,只执行一次,先于 ...

  5. margin为负值的几种情况

    1.margin-top为负值像素 margin-top为负值像素,偏移值相对于自身,其后元素受影响,见如下代码: 1 <!DOCTYPE html> 2 <html lang=&q ...

  6. Redis内存回收机制

    为什么需要内存回收? 原因有如下两点: 在 Redis 中,Set 指令可以指定 Key 的过期时间,当过期时间到达以后,Key 就失效了. Redis 是基于内存操作的,所有的数据都是保存在内存中, ...

  7. phpcms 增加备案号、联系方式等字段

    准备好记事本或者dreamweaver或者其它文本编辑器 打开\phpcms\languages\zh-cn\admin.lang.php PHPCMS的中文语言定义文件. 查找“site_manag ...

  8. vue移动端图片上传压缩

    上传压缩方法 import {api} from '../../api/api.js'; import axios from 'axios'; export function imgPreview ( ...

  9. JQuery多个异步操作后执行(resolve,promise,when,done)

    代码分享: //3秒后完成 function asyncThing1() { var dfd = $.Deferred(); setTimeout(function () { alert('async ...

  10. Channel 9视频整理【3】

    Will 保哥 微软mvp https://channel9.msdn.com/Niners/Will_Huang 繁体中文视频 Visual Studio 2017 新功能探索 https://ch ...