5.9 练习4-购物车系统

'''

4. 购物车系统

要求:

  1. 用户名和密码存放于文件中,格式为:nick|nick123
  2. 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
  3. 允许用户根据商品编号购买商品
  4. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  5. 可随时退出,退出时,打印已购买商品和余额
    '''

#商品列表
product_list = [
    ['Iphone7', 5800],
    ['Coffee', 30],
    ['water', 10],
    ['Python Book', 99],
    ['Bike', 199],
    ['ViVo X9', 2499],
]


#用户选择是否注册
register_choice = input("Register? y:register,any key: login >>")

#注册
if register_choice == 'y':
    print('Welcome to registration!!'.center(50, '-'))
    with open(r'1.user_info.txt', 'a', encoding='utf-8') as f_register_a:
        username_reg=input('username>>')
        password_reg=input('password>>')
        f_register_a.write(f'{username_reg}|{password_reg}\n')

#登录次数统计
count_log=0

#购物系统退出标识
flag=True

# 登录
while count_log<3:
    
    print('Welcome to login system!!'.center(50, '-'))
    username_log = input('username>>')
    password_log = input('password>>')

    # 定义字典,存储用户名和密码,方便验证登录
    userinfo_dict = dict()

    #记录商品购买次数
    goods_note=dict()

    #花费总金额
    overhead=0

    with open(r'1.user_info.txt', 'r', encoding='utf-8') as f_login_r:
        for i in f_login_r:
            user_lis = i.strip().split('|')
            userinfo_dict[user_lis[0]]= user_lis[1]

    if username_log in userinfo_dict and password_log == userinfo_dict[username_log]:
        print('Welcome to shopping system!!'.center(50, '-'))

        #输入用户余额
        balance = int(input('Please input your balance>>'))

        #获取商品序号和商品名称、价格
        for k,v in enumerate(product_list):
            print(f'{k}:{v}')

        while flag:
            print('-'*50)

            #输入要购买的商品序号
            goods_num = input('Please input your goods number,"q":quit>>')
            if goods_num=='q':break

            goods_num = int(goods_num)

            # 获取要购买的商品价格
            goods_money = product_list[goods_num][1]

            # 判断余额是否充足
            if balance - goods_money < 0:
                print('The balance is insufficient!!')
                print(f'Purchase history:{goods_note},You have spent:{overhead},Your balance is:{balance}')
                break
            #更新购买商品次数

            if product_list[goods_num][0] in goods_note:

                goods_note[product_list[goods_num][0]]+=1
            else:
                goods_note[product_list[goods_num][0]]=1




            # 更新余额
            overhead+=goods_money
            balance -= goods_money


            #用户选择是否继续购买
            goods_continue=input('Whether to continue to buy? n:quit,any key:continue>>')
            if goods_continue == 'n':
                print(f'购买记录:{goods_note},您已花费:{overhead},您的余额为:{balance}')
                flag = False
            else:
                continue
        break


    elif username_log not in userinfo_dict or password_log != userinfo_dict[username_log]:
        print('Wrong account password')
        count_log+=1
        if count_log==3:
          print('You have tried it three times, please try again later!!!')

运行结果1:

Register? y:register,any key: login >>y
------------Welcome to registration!!-------------
username>>blkj
password>>123
------------Welcome to login system!!-------------
username>>blkj
password>>123
-----------Welcome to shopping system!!-----------
Please input your balance>>200
0:['Iphone7', 5800]
1:['Coffee', 30]
2:['water', 10]
3:['Python Book', 99]
4:['Bike', 199]
5:['ViVo X9', 2499]
--------------------------------------------------
Please input your goods number,"q":quit>>4
Whether to continue to buy? n:quit,any key:continue>>y
--------------------------------------------------
Please input your goods number,"q":quit>>2
The balance is insufficient!!
Purchase history:{'Bike': 1},You have spent:199,Your balance is:1

运行结果2:

Register? y:register,any key: login >>m
------------Welcome to login system!!-------------
username>>123
password>>123
Wrong account password
------------Welcome to login system!!-------------
username>>123
password>>123
Wrong account password
------------Welcome to login system!!-------------
username>>123
password>>123
Wrong account password
You have tried it three times, please try again later!!!
posted @ 2025-08-18 15:13  bokebanla  阅读(4)  评论(0)    收藏  举报