项目要求:

1、增强版购物车是利用了文件系统,保存用户的购物列表和当前余额信息,在用户每次购物前先读取用户当前余额后再进行购买,并把购买的物品名称存到列表中;

2、用文件系统保存商品明细,不用每次都进行人工输入

程序要点:

1、文件读写操作

2、把一个格式化的文件内容读取到列表中,并对不需要的\n等信息进行处理,得到有效的格式化数据

待改进:

程序结构化不够,比较乱,后面可以函数化进行封装

'''用户入口:    1、商品信息存在文件里    2、已购商品,余额记录    商家入口:    1、可以添加商品,修改商品价格'''# 用户入口:# 从文件中读取所有商品信息到字典中

f_product_list = open(r"C:\Users\Administrator\PycharmProjects\S14Week2\product", 'r')f_shopping_list = open(r"C:\Users\Administrator\PycharmProjects\S14Week2\shoppinglist", 'r+')product_list = []shopping_list = []length = 0while True:    product_list_temp = f_product_list.readline()    # product_list_temp.strip()    if product_list_temp == "" or product_list_temp == "\n":        break    # print("RAW Line is :",product_list_temp)    product_list_temp = product_list_temp.strip()    # print("Strip result is :", product_list_temp)    product_list_temp = product_list_temp.split(",")    # print("Split result is :",product_list_temp)    # product_list_temp = product_list_temp.strip()    # print(product_list_temp)    product_list.append(product_list_temp)# print("Final result is :",product_list)product_list = list(product_list)salary = 0while True:    shopping_list_temp = f_shopping_list.readline()    if shopping_list_temp == "" or shopping_list_temp == "\n":        break    # print("RAW Line is :",product_list_temp)    shopping_list_temp = shopping_list_temp.strip()    # print("Strip result is :", product_list_temp)    shopping_list_temp = shopping_list_temp.split(",")    # print("Split result is :",product_list_temp)    # product_list_temp = product_list_temp.strip()    # print(product_list_temp)    shopping_list.append(shopping_list_temp)    salary = int(shopping_list_temp[1])

shopping_list = list(shopping_list)# print("Final result is :",product_list)print("Notice, this is salary:",salary)product_list = list(product_list)if salary == None:    salary = input("Input your salary:")    if salary.isdigit():        salary = int(salary)

while True:    for index, item in enumerate(product_list):        print(index, item)    choice = input("Input your choice:")    if choice.isdigit():        choice = int(choice)        if choice < len(product_list) and choice >= 0:            p_item = product_list[choice]            print(p_item)            if int(p_item[1]) <= salary:  # 买得起                shopping_list.append(p_item)                salary -= int(p_item[1])                f_shopping_list.write(p_item[0])                f_shopping_list.write(",")                f_shopping_list.write(str(salary))                f_shopping_list.write("\n")                print("Added %s into shoppint cart, your current balance is %s" % (p_item, salary))            else:                print("Sorry, your money is not enough!")        else:            print("Product is not exist")    elif choice == 'q':        print("Your current balance is ", salary)        f_shopping_list.close()        f_product_list.close()        break

转载于:https://www.cnblogs.com/huaweifisher/p/9460298.html

深夜十点半(三)——我的第三个Python程序“增强版购物车”相关推荐

  1. 华为云大数据存储的冗余方式是三副本_大数据显示华为云DDS增强版实感卓越

    数据库.操作系统.中间件并称为核心基础软件,在 IT 软件堆栈中起到中流砥柱的作用.今天运行的绝大多数企业应用软件都离不开数据库的支持. 随着移动互联网.物联网.云计算.大数据等新技术爆发式发展,图片 ...

  2. 十点半游戏python_[北京] APP Annie 招 Python 开发

    [北京] APP Annie 招 Python 开发攻城狮 nalanduanmu · 5 天前 · 278 次点击 APPEND [公司介绍] :APP Annie公司目前主要针对Ios和Andro ...

  3. python发牌代码十点半_深夜十点半(一)——我的第一个Python程序“登录系统”...

    系统设计要求: (1)可以输入用户名和密码 (2)密码要密文显示 (3)输错三次要锁定对应的用户 首先,我们来完成要求1,设计一个可以输入用户名和密码的界面,输入正确或错误进行提示. _usernam ...

  4. 深夜十点半(二)——我的第二个Python程序“购物车”

    这个程序的关键点为: 1.列表类型的数据结构设计 2.列表类型的数据结构查找方法 #购物车程序 #1. 启动后,输入用户工资 #2. 产品列表 product_list = [ ('Iphone',5 ...

  5. Maya Python 第三章 在Maya 中编写python程序

    3.2 Maya 命令 import maya.cmds as cmdssphere = cmds.polySphere() cmds.polySphere(sphere[1],edit=True,r ...

  6. 关于计算机系的三句半,网络流行语言俏皮话三句半.doc

    网络流行语言俏皮话三句半 网络流行语言俏皮话三句半 篇一:2016-尔雅通识课-用相声演绎中国文化试题及答案 满分尔雅答案 <用相声演绎中国文化>期末考试 一. 单选题(题数:50,共 5 ...

  7. 她一生三任伴侣,三次流产,却被誉为坠落凡间的天使,优雅到老

    美人在骨不在皮 作者:褚杨 著名导演比利.威尔德曾说:"上帝亲吻了一个小女孩的脸颊,于是赫本就诞生了." 奥黛丽.赫本,这个被誉为"坠落凡间的天使"的绝代佳人, ...

  8. 用Java求一个三位数,该三位数是与其每位数字的阶乘之和

    题目:一个三位数,该三位数与其每位数字的阶乘之和 代码: public class TestSum {public static void main(String[] args) {int a, b, ...

  9. iOS 11开发教程(三)运行第一个iOS 11程序

    iOS 11开发教程(三)运行第一个iOS 11程序 运行iOS11程序 创建好项目之后,就可以运行这个项目中的程序了.单击运行按钮,如果程序没有任何问题的话,会看到如图1.6和1.7的运行效果. 图 ...

最新文章

  1. 机器学习三要素之数据、模型、算法
  2. 中超赛程来100wan点in_不干了:中超球队改名“硬重启”,球迷组织绝望解散
  3. python常用模块大全总结-Python 常用模块大全(整理)
  4. Power-- 1.charge Fuel gauge
  5. 机器学习笔记:过拟合和欠拟合
  6. Java 如何线程间通信,面试被问哭。。。
  7. '0','\0',NULL,EOF的区别
  8. 虚拟机硬盘启动计算机后黑屏,高手亲自教告诉你win7虚拟机启动后黑屏的操作方案...
  9. bulter机器人_科普!九款使用率最高的物流机器人大盘点!
  10. bash: mail: command not found的解决方法
  11. Mac下配置sublime实现LaTeX
  12. python的取负运算_python 负数取模运算实例
  13. 海南可以禁燃油辆,东北不可以,因为冬天温度
  14. jQuery学习整理 (3) 使用jQuery操作元素的属性与样式
  15. Lucene2.4.0一般查询结果过滤与排行
  16. ubuntu安装gcc和g++
  17. 中英对照:30句经典英语广告词欣赏
  18. elementUI表格合并单元格详解
  19. linux php安装xsl扩展,11.32 php扩展模块安装
  20. 屏蔽google adsense和百度推广的广告

热门文章

  1. unity中监听文件夹并且创建文件夹后做资源更新
  2. Vue-cli3目录结构
  3. python 错误:KeyError
  4. [Codewar训练]Longest Common Subsequence(Performance version)(最长子序)
  5. 特别详细的POST 注入 思路
  6. 惊呆了!这个银行管理技巧也太厉害了吧
  7. 【交换篇】(6.4) ❀ 02. HA 状态下的核心交换机连接方法 (下) ❀ FortiSwitch 交换机
  8. 单元测试之Mockito
  9. ustc计算机科学与技术学院,连德富 - 中国科学技术大学 - 计算机科学与技术学院...
  10. NOJ电子老鼠闯迷宫