python作业购物车(第二周)

一.作业需求:

1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

7、允许查询之前的消费记录

二.思路

1.涉及的内容有文件操作,还有字典、列表、元组、等操作

2.将用户,密码,商城余额,购物商品,商品数量存成字典,保存数据,写入模块可以使用json,pickle

3.可以将用户,密码,余额写在一个文件,将另一个文件存该用户的购物车列表,也可以写在一起(我这里写在了一起)

4.将商品写成列表中的元组,例如: P_list = [('Iphone','5700')]

5.商城余额减去商品价格做判断,用文件操作字典,不够可以充值并修改字典文件的value值

6.将购物的商品列表最后写入到文件的字典中,方便下次查询使用例如:user_list[user]['salary']

三.流程图

待更新:

四.初始化用户信息代码:

user_list = {
    'test1': {
        'username': 'test000',
        'userpasswd': '123456',
        'salary': '',
        'shop_car': '',
        'shop_car_list': ''
    }
}

f = open('shopping_db', 'wb')
pickle.dump(user_list, f)
f.close()
View Code

五.购物车代码

f = open('shopping_db', 'rb') #读取字典操作
user_list = pickle.load(f)
f.close()

def wirte_logout():  #写入字典操作
    f = open('shopping_db', 'wb')
    pickle.dump(user_list, f)
    f.close()
    return
goods_list = [            #商品列表
    ('IPone', 5800),
    ('Mac Pro', 12000),
    ('MI盒子', 199),
    ('LG显示器23.6', 849)
]
def index_page():
    page = '''
[0]老用户登录 [1]新用户登陆 [2]浏览商品

'''
    welcome = ( '''33[32;1m欢迎登陆购物车作业系统33[0m''')
    print(welcome,'
',page)
    return

def login():
    print('
')
    global user
    count = 0
    while True:
        if count < 3:
            user = input('用户名: ')
            password = input('密 码: ')
            if user in user_list.keys():
                if user == user_list[user]['username'] and password == user_list[user]['userpasswd']:
                    print('
''欢迎登录!你的余额是:33[32;1m  %s33[0m' % user_list[user]['salary'],'
')
                    break
                else:
                    print('用户名或密码不正确,请重新登录!')
            else:
                user_choice = input('您不是老用户,是新用户的话按y写入系统按n重新登陆:(y/n)')
                if user_choice == 'y':
                    add_user()
                    break
                elif user_choice == 'n':
                    pass
            count += 1
        else:
            sys.exit('超出登录次数!登录失败')
    return


def add_user():
    global user
    exit_flag = False
    print('
')
    while not exit_flag:
        username = input('请输入你的用户名:')
        if username in user_list.keys():
            print('
用户名已存在,请输入其他名称
')
        else:
            exit_flag = True
    userpasswd = input('请输入你的密码')
    user_list[username] = {}
    user_list[username]['username'] = username
    user_list[username]['userpasswd'] = userpasswd
    user_list[username]['salary'] = ''
    user_list[username]['shop_car'] = ''
    user_list[username]['shop_car_list'] = ''
    print('
 %s,欢迎首次登陆请选购商品' % username, '
')
    user = user_list[username]['username']
    return

def print_product_list():
    print('
产品列表:
')
    for item in enumerate(goods_list):
        index = item[0]
        p_name = item[1][0]
        p_price = item[1][1]
        print(index, ':', p_name, p_price)
    return


def printending():
    print('之前购买的商品'.center(50, '-'))
    for item in user_list[user]['shop_car']:
        a = user_list[user]['shop_car'].index(item)
        print('33[32;1m  商品:%s  价格:%s  数量:%s 33[0m' % (
        user_list[user]['shop_car'][a][0], user_list[user]['shop_car'][a][1], user_list[user]['shop_car_list'][a]))
    print('End'.center(58, '-'))
    print('33[32;1m  余额是:%s33[0m' % user_list[user]['salary'])
    print('End'.center(58,'-'),'
')
    return



exit_flag = False

while not exit_flag:
    index_page()

    index_user_choice = input('请输入您要进行的操作:')

    if index_user_choice == '0':
        login()
        exit_flag = True
    elif index_user_choice == '1':
        add_user()
        exit_flag = True
    elif index_user_choice == '2':
        print_product_list()
    else:
        print('输入操作无效!')

print('Begin The Shopping'.center(80, '-'), '
')

exit_flag2 = False

while not exit_flag2:
    if not user_list[user]['salary']:
        salary = input('你的余额为0,请输入金额: ')
        if salary.isdigit():
            salary = int(salary)
            user_list[user]['salary'] = salary
        else:
            print('数据类型不正确,请输入一个正确的数字!')
            continue
    else:
        salary = user_list[user]['salary']
    if not user_list[user]['shop_car']:
        shop_car = []
        shop_car_list = []
    else:
        shop_car = user_list[user]['shop_car']
        shop_car_list = user_list[user]['shop_car_list']
    print_product_list()
    print('''
[q=quit,c=check,l=logout,r=rechage]
''')
    user_choice = input('请问你想购买什么?')
    print (shop_car)
    if user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(goods_list):
            p_item = goods_list[user_choice]
            if p_item[1] <= salary:
                if p_item not in shop_car:
                    shop_car.append(p_item)
                    shop_car_list.append(1)
                    user_list[user]['shop_car'] = shop_car
                    user_list[user]['shop_car_list'] = shop_car_list
                    salary -= p_item[1]
                    user_list[user]['salary'] = salary
                    print('33[32;1m 添加了:%s,价格是:%s,余额:%s33[0m' % (p_item[0], p_item[1], user_list[user]['salary']), '
')
                else:
                    item_num = shop_car_list[shop_car.index(p_item)] + 1
                    shop_car_list[shop_car.index(p_item)] = item_num
                    user_list[user]['shop_car_list'] = shop_car_list
                    salary -= p_item[1]
                    user_list[user]['salary'] = salary
                    print('33[32;1m 在购物车上添加了:%s,价格是:%s,你的余额是:%s33[0m' % (p_item[0], p_item[1], user_list[user]['salary']), '
')
            else:
                print('33[32;1m你的余额是: %s,余额不足购买!33[0m' % user_list[user]['salary'], '
')
        else:
            print('你选择的商品不存在!', '
')
    else:
        if user_choice == 'q' or user_choice == 'quit':
            printending()
            print('Bye %s' % user)
            wirte_logout()
            exit_flag2 = True
        elif user_choice == 'c' or user_choice == 'check':
            printending()
        elif user_choice == 'l' or user_choice == 'logout':
            printending()
            login()
        elif user_choice == 'r' or user_choice == 'rechage':
            salary_change = input('请输入充值金额:')
            if salary_change.isdigit():
                salary_change = int(salary_change)
                salary += salary_change
                user_list[user]['salary'] = salary
            else:
                print('数据类型不正确,请输入一个正确的数字!')
                continue
        else:
            print('请输入合法字符!', '
')
View Code
原文地址:https://www.cnblogs.com/sean-yao/p/7702389.html