第五篇:python购物车小程序开发demo
功能:
自定义工资水平,可选商品加购
余额实时提醒
用到的知识点:
列表、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的更多相关文章
- python 购物车小程序
python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...
- 微信小程序开发demo
自己写的小程序,欢迎下载 https://gitee.com/lijunchengit/chengZiShengHuoBang
- python 购物车小程序(列表、循环、条件语句)
goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...
- 第五篇、微信小程序-swiper组件
常用属性: 效果图: swiper.wxml添加代码: <swiper indicator-dots="{{indicatorDots}}" autoplay="{ ...
- 微信小程序开发-入门到熟练(wepy-初级篇)
Title:最近做完了项目,review代码的同时,就想写一篇详细的小程序开发经历,记录自己的项目从0到1的过程 Desc : 小程序从0到1,从小白到完成项目,你需要这样做: step1: 基础知识 ...
- 【微信小程序开发】全局配置
今天看看小程序全局配置. 上一篇[微信小程序开发]秒懂,架构及框架 配置,无非就是为了增加框架的灵活性,而定下的规则. 微信小程序的配置文件是一个树状结构,各个节点代表不同的配置项,小程序框架会解析这 ...
- 【微信】2.微信小程序开发--官方开发工具使用说明
承接第一篇 =============================================== 关于微信小程序开发使用IDE,曾经自己动摇过. 到底是采用 微信官方小程序开发工具 WebS ...
- 微信小程序开发系列二:微信小程序的视图设计
大家如果跟着我第一篇文章 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 一起动手,那么微信小程序的开发环境一定搭好了.效果就是能把该小程序的体验版以二维码的方式发送给其他朋友使用. 这个系列 ...
- 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)
七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...
随机推荐
- P1098 方程解的个数
题目描述 给出一个正整数N,请你求出x+y+z=N这个方程的正整数解的组数(1<=x<=y<=z<1000).其中,1<=x<=y<=z<=N . 输入 ...
- win10 uwp win2d CanvasVirtualControl 与 CanvasAnimatedControl
本文来告诉大家 CanvasVirtualControl ,在什么时候使用这个控件. 在之前的入门教程win10 uwp win2d 入门 看这一篇就够了我直接用的是CanvasControl,实际上 ...
- linux ioctl 方法
ioctl, 我们在第 1 章展示给你如何使用, 是一个系统调用, 作用于一个文件描述符; 它 接收一个确定要进行的命令的数字和(可选地)另一个参数, 常常是一个指针. 作为一个使 用 /proc 文 ...
- linux 在 /proc 里实现文件
所有使用 /proc 的模块应当包含 <linux/proc_fs.h> 来定义正确的函数. 要创建一个只读 /proc 文件, 你的驱动必须实现一个函数来在文件被读时产生数据. 当 某个 ...
- while循环&CPU占用率高问题深入分析与解决方案
直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方.使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果, ...
- 2018-10-19-C#-AddRange-添加位置
title author date CreateTime categories C# AddRange 添加位置 lindexi 2018-10-19 9:3:8 +0800 2018-2-13 17 ...
- 【33.18%】【hdu 5877】Weak Pair (3种解法)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...
- 如何用python“优雅的”调用有道翻译?
前言 其实在以前就盯上有道翻译了的,但是由于时间问题一直没有研究(我的骚操作还在后面,记得关注),本文主要讲解如何用python调用有道翻译,讲解这个爬虫与有道翻译的js“斗争”的过程! 当然,本文仅 ...
- 2019-10-12-win10-uwp-非ui线程访问-ui-
title author date CreateTime categories win10 uwp 非ui线程访问 ui lindexi 2019-10-12 15:0:12 +0800 2018-2 ...
- 洛谷——P1012拼数字符串操作(拼接排序)
#include<bits/stdc++.h> using namespace std; bool cmp(const string &a,const string &b) ...