python学习,第二周作业:完善购物车程序


#本作业涉及知识点:对文件的读取操作,读取到字符串转换成列表,及二级列表生成。

#作业存在的不完善:对文件操作了解的比较少,对于按行读取,按行追加文件的内容还没理清楚。期待下周学习可以搞定。

#读文件,生产产品列表 file_path ='D:\\Python\\project\\Productinfo.txt' addlist=[] productlist=[] producttotallist=[] shopinglist = [] f = open(file_path,'r+') while True: line = (f.readline()).strip() if not line: break productlist.append(line) if(len(productlist)== 2): producttotallist.append(productlist) productlist =[] print(producttotallist) f.close() while True: UpdateProductlist = input("是否需要添加商品或者修改商品价格:") if(UpdateProductlist != 'y'): salary = input("请输入用于购买商品的总金额:") salary = int(salary) #客户购买商品流程 while True: choice = input("请输入购买商品的编号:") if choice.isdigit(): choice = int(choice) if choice>= 0 and choice < len(producttotallist): if(salary >= int(producttotallist[choice][1])): salary -= int(producttotallist[choice][1]) shopinglist.append(producttotallist[choice]) else: print("账户余额不足,退出购买") print("用户已购商品:{shopinglist}".format(shopinglist=shopinglist)) print("用户账户余额:{salary}".format(salary=salary)) break else: print("输入产品编号不存在") elif choice == 'q': print("购买完成退出") print("用户已购商品:{shopinglist}".format(shopinglist=shopinglist)) print("用户账户余额:{salary}".format(salary=salary)) break else: print("输入产品信息错误,请重新输入!!") else: addupdate = input("是添加还是修改?") if addupdate == 'add' : f = open(file_path,'r+') addproduct = input("输入添加商品名称:") addproductprice = input("输入添加商品价格:") addlist.append(addproduct) addlist.append(addproductprice) producttotallist.append(addlist) print(producttotallist) else: updateNo = input("输入修改商品编号:") updatepice = input("输入修改商品编号:") updateNo = int(updateNo) if(updateNo >=0 and updateNo < len(producttotallist)): producttotallist[updateNo][1]= updatepice print(producttotallist) else: print("修改商品编号输入错误")

猜你喜欢

转载自www.cnblogs.com/fubaodemaPythonStudy/p/9655637.html
今日推荐