第二周 购物车


购物车程序:
1、启动程序后,输入用户名密码后,如果是第一 次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中,关键输出, 如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买7、允许查询之前的消费记录

# with open('userinfo', mode='w+', encoding='utf-8')as f:
# f.write('1251865477 123456')

# def login():
# i = 0
# li = []
# while i < 3:
# username = input('请输入你的账号:')
# password = input('请输入你的密码:')
# with open('userinfo', mode='r+', encoding='utf-8')as f:
# for line in f:
# li.append(line)
#
# if username.strip() == li[0].strip() and password == li[1].strip():
# print('登陆成功')
# break
# else:
# print('用户名或密码错误,请重试')
# i += 1
#
# else:
# print('用户密码输入错误三次,账号已锁定')

#指定的一个账号和密码

print('---------欢迎登录浣溪谷购物广场---------')
username = input('请输入你的用户名:')
password = input('请输入你的账号密码')

if username =='hanfei' and password == '123456': #如果用户名密码正确 进入上次消费的清单
    with open('info.txt',mode='r',encoding='utf-8')as f,
        open('price.txt',mode='r',encoding='utf-8')as p:
        f=f.read()
        p=p.read()
        print('###########你的购物清单是###########')
        print('%s'%f,'余额:%s'%p)

product_list=[
    ('mac pro',9800),
    ('apple watch',15800),
    ('Bike',800),
    ('coffee',31),
    ('python class',20000)
]

shopping_list=[]
salary= input('Input your salary:')

if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list): #enumerate 枚举,index可获取下标
            print(index+1,item)
        user_choice=input('请选择你的购买商品的编号:')
        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice<len(product_list):
                print(product_list[user_choice-1])

                if salary>=product_list[user_choice-1][1]: #买的起
                    shopping_list.append(product_list[user_choice-1][0])  #加入购物车
                    salary-=product_list[user_choice-1][1]  #加入购物车就减掉

                    print('已加到购物车,您的余额为:33[31;1m%s33[0m' % salary)

                else:print('33[41;1m余额不足,请及时充值!33[0m')
            else:
                print('invalid ...')
        elif user_choice == 'q':
            print('-----------您已购买如下商品-----------')
            # print(shopping_list)
            for i in shopping_list:
                print(i)
                with open('info.txt','a+',encoding='utf-8') as f1:
                    f1.write('商品:'+i+'
')

            print('您还剩33[31;1m%s33[0m'%salary)
            with open('price.txt',mode='w',encoding='utf-8')as f2:
                f2.write('余额:'+str(salary))
            print('33[42;1m-----欢迎下次光临-----33[0m')
            exit()
        else:
            print('输入有误')
原文地址:https://www.cnblogs.com/hanfe1/p/10794929.html