优秀校园网站建设汇报,个人备案的网站,网站在百度的图标显示不正常,天津网站设计建设Python3实现购物车功能来源#xff1a;中文源码网 浏览#xff1a; 次 日期#xff1a;2018年9月2日【下载文档: Python3实现购物车功能.txt 】(友情提示:右键点上行txt文档名-目标另存为)Python3实现购物车功能 本文实例为大家分享了Python3实现购物车功能的具体…Python3实现购物车功能来源中文源码网 浏览 次 日期2018年9月2日【下载文档: Python3实现购物车功能.txt 】(友情提示:右键点上行txt文档名-目标另存为)Python3实现购物车功能 本文实例为大家分享了Python3实现购物车功能的具体代码供大家参考具体内容如下购物车要求1、启动程序后输入用户名密码后如果是第一次登录让用户输入工资然后打印商品列表2、允许用户根据商品编号购买商品3、用户选择商品后检测余额是否够够就直接扣款不够就提醒 4、可随时退出退出时打印已购买商品和余额5、在用户使用过程中 关键输出如余额商品已加入购物车等消息需高亮显示6、用户下一次登录后输入用户名密码直接回到上次的状态即上次消费的余额什么的还是那些再次登录可继续购买7、允许查询之前的消费记录逻辑图执行代码#!/usr/bin/env python3# Author: Robert# --*-- coding: utf-8 --*--set False #设置set 当输入为q就可以退出file open(购物车用户信息档案.txt,r,encodingutf-8) #读取购物车用户信息文件f str(file.read()) #将文件内容转化成字符串for line in f:file_str str(f)data eval(file_str) #将字符串转换为字典dataname input(输入姓名)password input(输入密码)while True:if name in data: #用户在档案中if password in data[name]: #密码和用户名对应校验正确登录。salary float(data[name][password])print(\033[32;1m欢迎登录当前余额为%s\033[0m%salary)breakelse: #否则密码错误请重新输入password input(密码错误请重新输入)continueelse: #否则判断为首次登录将用户名密码工资存到用户信息文件中password_salary {}salary_str input(欢迎首次登录请输入你的工资)salary float(salary_str)password_salary[password] salary #工资对应到密码data[name] password_salary #将密码-工资对应到用户名file.seek(0)file.write(str(data))file.tell()breaklist [#购物清单[iphone,5800],[sifei,800],[macbook,17500],[book,75],[apple,5]]file_list_r open(历史购物信息.txt,r,encodingutf-8)file_list_r str(file_list_r.read())shoppinglist_dict eval(file_list_r)if name not in shoppinglist_dict:shoppinglist_dict[name] []shoppinglist shoppinglist_dict[name]shoppinglist_dict_now []choose input(\n是否需要查询历史购物记录(y/n))if choose y:print(\n\n----------历史购物记录 print(shoppinglist)print(----------结束 print(----------商品清单 for index,item in enumerate(list,1):print(index,item)print(----------结束 number input(请输入想购买商品编号)if number q:set Truedata[name][password] str(salary)file.seek(0)file.write(str(data))file.tell()print(----------购物清单 print(shoppinglist)print(您的余额,salary)print(----------结束 shoppinglist.extend(shoppinglist)shoppinglist_dict[name] shoppinglistelif number.isdigit() False:print(\033[31;1m输入不是编号内容请重新输入\033[0m)elif int(number)int(len(list)) or int(number) 0: #输入值不在清单中报错print(\033[31;1m您所购买的商品不在清单中\033[0m)else:number_buy int(number)-1if list[number_buy][1] salary salary - int(list[number_buy][1])msg \033[32;1m您已经将%s加入购物车中余额为%d\033[0m%(list[number_buy][0],salary)print(msg)shoppinglist.append(list[number_buy]) #将本次购物信息加到购买记录中else:print(\033[31;1m余额不足无法购买!\033[0m) #提示余额不足购物车用户信息档案.txt{name: {password: 10000}, cx: {123: 725.0}, robert: {qw: 440.0}, cv1: {1: 100.5}, ROBERT: {QW: 1560.0}, qwe: {qw: 1555.0}}历史购物信息.txt{name: [[iphone, 5800],[bike, 800]], cx:[[iphone, 5800],[apple, 5],[apple, 5], [book, 75]]}以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持中文源码网。亲,试试微信扫码分享本页! *^_^*