超市购物简例
来源:互联网 发布:java gbk乱码 编辑:程序博客网 时间:2024/06/11 10:00
购物例子
#!/usr/bin/env python# -*- coding:utf-8 -*-#Author:xp#blog_url: http://blog.csdn.net/wuxingpu5/article/details/71209731#文件名对应关系 用户 userinfo #商品 comm #购买记录history#用户登录cash = ''user_file = open('userinfo', 'r', encoding='utf-8')user_data = user_file.read()user_file.close()user_list = user_data.split('\n')list_info_user = []for item in user_list: user_temp = item.split('|') dict_temp = { 'name': user_temp[0], 'pwd': user_temp[1], 'times': user_temp[2], 'money': user_temp[3] } list_info_user.append(dict_temp)username = input('Input username: ')passwd = input('Input passwd: ')for item in list_info_user: if username == item['name'] and passwd == item['pwd'] and int(item['times']) !=3: print('login success') cash = item['money'] print(cash) break#查看商品信息comm_file = open('comm', 'r', encoding='utf-8')comm_data = comm_file.read()comm_file.close()#商品列表comm_list = comm_data.split('\n')#转换为列表字典嵌套comm_list_all = []for item in comm_list: val1 = item.split('|') val1_dict = { 'num': val1[0], 'name': val1[1], 'price': val1[2], } comm_list_all.append(val1_dict)#输入页码 分页显示内容val = input('请输入要查看商品的页码: ')p = int(val)start_page = (p-1)*10end_page = p*10user_select = comm_list_all[start_page:end_page]print('第', p, '页的商品内容为', user_select)sele = input('请选择商品编号: ')select_num = str(sele)len_user_select = len(select_num)print('选择了', len_user_select, '件商品')#sum_price 购买商品总价格 #sum_comm 购买的所有商品i = 0sum_price = 0sum_comm = []while i < int(len_user_select): comm_each = user_select[int(select_num[i])] sum_comm.append(comm_each) price = int(user_select[i - 1]['price']) sum_price += price i += 1print('sum price is:', sum_price)print('购买的所有商品: ', sum_comm)cash = int(cash)#判断是否购买成功if cash >= sum_price: print('购买成功')else: print('余额不足请充值')#购买记录写入文件#def history_comm():i = 0target = ''for i in sum_comm: print(i) target_temp = i['num'] + '|' + i['name'] + '|' + i['price'] target = target + target_temp + '\n'print ('此次购买商品为: ',target)history_file = open('history', 'a', encoding='utf-8')history_file.write(target)history_file.close()#查询已购买商品sear = input('是否查询已购买商品,请输入y继续或者任意字符取消')if sear == 'y' or sear == 'Y': history = open('history', 'r', encoding='utf-8') data = history.read() history.close() hist_list = data.split('\n') n = 0 targ = '' while n < len(hist_list): targ = targ + hist_list[n] n += 1 sear_name = input('输入需要查询的名字: ') print('查询:', sear_name,'在', hist_list) if sear_name in targ: print('购买过') else: print('这个没买过')else: print('exit') exit()
userinfo文件内容
wuxp|123|0|2000obama|123|0|3000comm文件内容
1|电脑|19992|鼠标|193|游艇|614|美女|165|娃娃|126|手机|107|手表|188|键盘|799|糖水|4910|眼镜|3911|饼干|2912|可乐|913|鸡翅|1914|香蕉|115|黄瓜|19916|水杯|199
history文件
0 0
- 超市购物简例
- 超市购物
- 超市购物预测
- 超市购物管理系统
- 超市购物系统
- CSU 1086 超市购物
- csu----超市购物
- CSU1086-超市购物-分组背包
- 超市购物小票案例
- CSU 1086 超市购物 分组背包问题
- 探索性测试与超市购物
- C#控制台版超市购物系统
- csu1086(超市购物)(分组背包)
- 从沈阳超市的购物车谈起
- C#练习--实现超市购物结算
- 不用超市免费塑料袋 新玛特绿色通道引领环保购物
- 【UML】案例分析:新型超市购物自助系统
- 关联规则挖掘之Apriori算法实现超市购物
- 简单实现上拉下拉,增加头条目和尾条目的RecyclerView
- [UE4]注意事项,插件中蓝图方法库在C++中的使用
- Group by 总结 时光慢点走~~
- 感动哭!Starbreeze发布《收获日2》VR版
- Xcode 找不到头文件
- 超市购物简例
- python2.7安装模块
- Log4j2日志配置
- 网络监控工具ntopng
- centos7安装jdk+tomcat+nginx+mysql
- [leetcode: Python]167. Two Sum2
- easyui 属性
- eclipse实用快捷键
- PCA