超市购物简例

来源:互联网 发布: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|3000
comm文件内容

1|电脑|19992|鼠标|193|游艇|614|美女|165|娃娃|126|手机|107|手表|188|键盘|799|糖水|4910|眼镜|3911|饼干|2912|可乐|913|鸡翅|1914|香蕉|115|黄瓜|19916|水杯|199

history文件



0 0