• 购物车登陆购物版


    *****************购物车
    import os
    product_list = [['Iphone7',5800],
    ['Coffee',30],
    ['疙瘩汤',10],
    ['Python Book',99],
    ['Bike',199],
    ['ViVo X9',2499],

    ]

    shopping_cart={}
    current_userinfo=[]
    db_file=r'db.txt'
    while True:
    print('''
    1 注册
    2 登录
    3 购物
    ''')
    choice=input('请选择:').strip()
    if choice =='1':
    while True:
    name=input('username:')
    password=input('password:')
    conf_password=input('conf password')
    balance=input('balance:')
    if password ==conf_password:
    with open(db_file,'a') as f:
    f.write('%s,%s,%s '%(name,password,balance))
    break
    else:
    print('两次密码不一致')
    elif choice =='2':
    count=0
    tag=True
    while tag:
    if count ==3:
    print('错误过多,退出')
    break
    name=input('name')
    password=input('password')
    with open(db_file,'r') as f:
    for line in f:
    user_info=line.split(',')
    user_name=user_info[0]
    user_password=user_info[1]
    user_balance=int(user_info[2])
    if user_name == name and user_password == password:
    current_userinfo=[user_name,user_balance]
    print('登录成功')
    print('用户信息为:',current_userinfo)
    tag=False
    break
    else:
    print('用户名密码错误')
    count+=1


    elif choice == '3':
    if len(current_userinfo) == 0:
    print('33[49m请先登陆...33[0m')
    else:
    #登陆成功后,开始购物
    uname_of_db=current_userinfo[0]
    balance_of_db=current_userinfo[1]

    print('尊敬的用户[%s] 您的余额为[%s],祝您购物愉快' %(
    uname_of_db,
    balance_of_db
    ))

    tag=True
    while tag:
    for index,product in enumerate(product_list):
    print(index,product)
    choice=input('输入商品编号购物,输入q退出>>: ').strip()
    if choice.isdigit():
    choice=int(choice)
    if choice < 0 or choice >= len(product_list):continue

    pname=product_list[choice][0]
    pprice=product_list[choice][1]
    if balance_of_db > pprice:
    if pname in shopping_cart: # 原来已经购买过
    shopping_cart[pname]['count']+=1
    else:
    shopping_cart[pname]={'pprice':pprice,'count':1}

    balance_of_db-=pprice # 扣钱
    current_userinfo[1]=balance_of_db # 更新用户余额
    print(pname + " 添加到购物车,余额为: " + str(balance_of_db))

    else:
    print("产品价格是{price},你还差{lack_price}".format(
    price=pprice,
    lack_price=(pprice - balance_of_db)
    ))
    print(shopping_cart)
    elif choice == 'q':
    print("""
    ---------------------------------已购买商品列表---------------------------------
    id 商品 数量 单价 总价
    """)

    total_cost=0
    for i,key in enumerate(shopping_cart):
    print('%22s%18s%18s%18s%18s' %(
    i,
    key,
    shopping_cart[key]['count'],
    shopping_cart[key]['pprice'],
    shopping_cart[key]['pprice'] * shopping_cart[key]['count']
    ))
    total_cost+=shopping_cart[key]['pprice'] * shopping_cart[key]['count']

    print("""
    您的总花费为: %s
    您的余额为: %s
    ---------------------------------end---------------------------------
    """ %(total_cost,balance_of_db))

    while tag:
    inp=input('确认购买(yes/no?)>>: ').strip()
    if inp not in ['Y','N','y','n','yes','no']:continue
    if inp in ['Y','y','yes']:
    # 将余额写入文件

    src_file=db_file
    dst_file=r'%s.swap' %db_file
    with open(src_file,'r',encoding='utf-8') as read_f,
    open(dst_file,'w',encoding='utf-8') as write_f:
    for line in read_f:
    if line.startswith(uname_of_db):
    l=line.strip(' ').split(',')
    l[-1]=str(balance_of_db)
    line=','.join(l)+' '

    write_f.write(line)
    os.remove(src_file)
    os.rename(dst_file,src_file)

    print('购买成功,请耐心等待发货')

    shopping_cart={}
    current_userinfo=[]
    tag=False
    else:
    print('非法输入')
  • 相关阅读:
    linux的redis的安装和使用
    在linux上安装Mysql和使用
    linux的python3的安装
    linux的优化和命令
    LeetCode 542. 01 Matrix
    Qt keyPressEvent
    QOpenGLTexture 两个纹理叠加
    Qt5.6.0+OpenGL 纹理贴图首战告捷
    更改Qt Application为 Qt Console Application
    Learning part-based templates from large collections of 3D shapse CorrsTmplt Kim 代码调试
  • 原文地址:https://www.cnblogs.com/wangcheng9418/p/9150829.html
Copyright © 2020-2023  润新知