功能:
自定义工资水平,可选商品加购
余额实时提醒
用到的知识点:
列表、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. 【codeforces 789A】Anastasia and pebbles

    [题目链接]:http://codeforces.com/contest/789/problem/A [题意] 有n种物品,每种物品有wi个; 你有两个口袋,每个口袋最多装k个物品; 且口袋里面只能装 ...

  2. H3C 示例:根据主机地址数划分子网

  3. 【t091】油滴扩展

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一个长方形框子里,最多有N(0≤N≤6)个相异的点.在其中任何一个点上放一个很小的油滴,那么这个油滴 ...

  4. jQuery---鼠标滚轮控制div横向滚动条左右移动

    HTML <div class="table-responsive"> <div class="fhtable" style="wi ...

  5. MobaXterm 使用中间服务器

    经常需要连接服务器,但是有时候服务器需要经过一层中间服务器才可以连接,所以本文告诉大家如何使用MobaXterm 配置中间服务器,进行ssh连接 在本文的开始,本地转发服务器已经弄好,本文不会告诉大家 ...

  6. [USACO10OCT]Lake Counting(DFS)

    很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...

  7. koa2入门--01.ES6简单复习、koa2安装以及例子

    1.ES6简单复习 /*let 和 const: let用于定义一个块作用域的变量,const 定义一个常量 */ let a = 'test'; const b = 2; /*对象的属性和方法的简写 ...

  8. SmartAssembly 使用方法

    SmartAssembly加壳工具,我还真的是不太喜欢给自己的程序加壳,觉得开源才是王道,但是没办法工作需要,需要有个加壳后与加壳前的对比,好吧谁叫咱只是程序员呢. 开始埋头苦干,找了半天也没找到合适 ...

  9. SQL常见命令

    SQLite常见命令:https://www.cnblogs.com/senior-engineer/p/7028972.html

  10. Linux下搭建实现HttpRunnerManager的异步执行、定时任务及任务监控

    前言 在之前搭建的HttpRunnerManager接口测试平台,我们还有一些功能没有实现,比如异步执行.定时任务.任务监控等,要完成异步执行,需要搭建 RabbitMQ 等环境,今天我们就来实现这些 ...