首页 / PYTHON / 从零单排Python学习第二课
从零单排Python学习第二课
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从零单排Python学习第二课,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2536字,纯文字阅读大概需要4分钟。
内容图文
简易购物车:
1,商品展示
2,添加商品到购物车
#list集合保存商品信息
product_list = [("电视机",5000), ("洗衣机",3000), ("电冰箱",2000), ("电吹风",1000), ("电风扇",300), ("热水器",2900)]
#购物车数据 shopping_car = []
#当前余额 salary = 100000
#打印出商品信息 for index,item in enumerate(product_list): print(index,item) while True: print("---当前余额> %s 输入‘q‘退出程序 输入编号加入购物车---" % (salary)) choice = input("输入商品编号>>") if choice.isdigit():#判断输入是否是编号 choice = int(choice) if choice < len(product_list) and choice >= 0:#判断是否在下标范围内if salary > product_list[choice][1]:#判断余额大于商品售价 salary = salary - product_list[choice][1]#计算余额 shopping_car.append(product_list[choice])#加入购物车 print("购物车商品>", shopping_car) else: print("余额不足") else: print("商品不存在") elif choice=="q": print("退出程序 》 购物车商品>", shopping_car) breakelse: print("输入错误") break
购物车增强版↓↓
商品信息保存在文件中
增加卖家入口 商品可以添加 改价格
with open("product.txt") as f: product_list = list(eval(f.readline()))
#product.txt文件如下
[("电视机",5000),("洗衣机",3000),("电冰箱",2000),("电吹风",1000),("电风扇",300),("热水器",2900)]
# Author ml with open( " product.txt ") as f:#读取商品列表 product_list = list(eval(f.readline())) shopping_car = [] salary = 100000 while True: print("---当前余额> %s 输入‘q‘退出程序 输入‘e‘进入编辑模式 输入编号加入购物车---" % (salary)) for index, item in enumerate(product_list): print(index, item)#商品展示 choice = input("输入商品编号加入购物车>>") if choice.isdigit():#判断输入是否是编号 mode= 0 choice = int(choice) if choice < len(product_list) and choice >= 0:#判断是否在下标范围内if salary > product_list[choice][1]:#判断余额大于商品售价 salary = salary - product_list[choice][1] shopping_car.append(product_list[choice]) print("购物车商品>", shopping_car) else: print("余额不足") else: print("商品不存在") elif choice=="q": print("退出程序 》 购物车商品>", shopping_car) breakelif choice=="e": mode = 1 print("正在编辑模式 添加商品请输入 a","编辑商品价格请输入编号") order = input(">>>") if order.isdigit(): order = int(order) if order < len(product_list) and order >= 0: # 判断是否在下标范围内print(product_list[order]) new_price = int(input("请输入价格>>")) if new_price>0: new_tuple = (product_list[order][0],new_price) product_list.insert(order,new_tuple) del product_list[order+1] with open("product.txt", "w") as f: f.write(str(product_list)) else: print("输入价格不合法") elif order == "a": add_product = input("请输入商品名称及价格用‘/‘分割")#没有对新增加商品正确性做判断 new_tuple = tuple((add_product.split("/")[0],int(add_product.split("/")[1]))) product_list.insert(len(product_list),new_tuple) with open("product.txt", "w") as f: f.write(str(product_list)) else: print("输入错误") break
运行图↓↓
原文:https://www.cnblogs.com/limiaolei/p/9651930.html
内容总结
以上是互联网集市为您收集整理的从零单排Python学习第二课全部内容,希望文章能够帮你解决从零单排Python学习第二课所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。