Week3

购物车
    用户入口:
    1.商品信息存在文件里
    2.已购商品, 余额记录

import os

product_list = []
user_login_flag = False
shop_list = []


#读取商品信息

if (os.path.exists("product_info.txt")):
    f = open("product_info.txt", "r")
    for line in f:
        index = 0
        line = line.strip('\n')
        product_info = line.split(":")
        product_list.append(product_info)
        index =index + 1
    f.close()
else:
    print("no product info. exit!")
    exit(1)

#print(product_list)
#登录购物车,获取余额记录

uname = input("please input your name:")
passwd = input("input your password:")

if (os.path.exists("shopchart_users.txt")):
    f = open("shopchart_users.txt", "r")
    for line in f:
        index = 0
        line = line.strip('\n')
        userinfo = line.split(":")
        if((userinfo[0] == uname)
                and (userinfo[1] == passwd)):
            if userinfo[2].isdigit():
                salary = userinfo[2]
                user_login_flag = True
                #print(salary)
    f.close()
else:
    print("No user info. exit!")
    exit(2)



#打印已购商品和余额记录
if user_login_flag == True:
    #获取已购商品列表
    if (os.path.exists("shopchart.txt")):
        f = open("shopchart.txt", "r")
        for line in f:
            index = 0
            line = line.strip('\n')
            shop_list = line.split(":")
            if shop_list[0] == uname:
               shop_cart = shop_list[1:]
        f.close()
    else:
        print("No user info. exit!")
        exit(2)
    print("salary is:", salary)
    print("My shop cart list:", shop_cart)
 
 

猜你喜欢

转载自www.cnblogs.com/thinboy-programmer/p/9657608.html