购物车小程序~

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

while 条件:       while后面的条件成立,就执行下面的代码
    执行代码...

index() 函数用于从列表中找出某个值第一个匹配项的索引位置。
例如:
aList = [123, 'xyz', 'runoob', 'abc']
print "xyz 索引位置: ", aList.index( 'xyz' )
print "runoob 索引位置 : ", aList.index( 'runoob', 1, 3 )
输出:
xyz 索引位置:  1
runoob 索引位置 :  2
"""
import os
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
{"name": "苹果", "price": 20},
{"name": "酸奶", "price": 10},
{"name": "玉米", "price": 5},
]
name = ['ljy']
password = ['123456']
goods_list = [] #用户的购物车
flog = True
money_path = './money.txt'
path = './shoplist.txt'

def xargs_file(msg):
    if os.path.exists(path):     #判断文件是否存在
        f = open(path, 'a')
        f.write(str(msg))
        f.write(',')
        f.close()
    else:
        f = open(path, 'w')
        f.close()
def select_money(msg):
    if os.path.exists(money_path):  #判断文件是否存在
        f = open(money_path, 'r')
        money=f.read()
        f.close()
        return money
    else:
        f = open(money_path, 'w')
        f.write(str(msg))
        f.close()

while flog:
    Uname = input("请输入用户名:").strip()  #str.strip():去除字符串两边的空格
    Upassword = input("请输入用户密码:").strip()
    if Uname == name[0] and Upassword == password[0]:
        print("登录成功!")
        if os.path.exists(money_path):
            salary=int(select_money(None))
        else:
            salary = int(input("请输入工资:"))
            select_money(salary)

        while flog:
            for index,i in enumerate(goods):
                print(index,i)  #打印出了带索引的商品列表
            choice = input("请输入要选择的商品编号:")
            if choice.isdigit():
                choice = int(choice)  #接上,如果选择的商品编号是数字类型,那么就成功赋值给选择的编号
                if 0 <= choice < len(goods):    #选择的编号在物品清单的索引范围内
                    if salary >= goods[choice].get('price'):  #如果用户的工资大于等于 在商品列表里所选择的编号对应的价格
                        salary = salary - goods[choice].get('price')  #用户的工资=工资-商品列表里所选择的编号对应的价格
                        goods_list.append(goods[choice])  #用户的购物车末尾增加这个选择了的东西
                        print("用户的购物车里有:%s"%goods_list)
                        print("用户的余额还有:%s"%salary)
                    else:
                        print("用户余额不足,还剩%s"%salary)
                else:
                    print("用户输入的商品编号不在物品清单范围内")
            elif choice == 'q':   #保存并退出
                print('---------购物车列表---------')
                for index in goods_list:
                    xargs_file(index)
                    print(index)  #显示用户购买过的购物车
                f = open(path,'r')
                shop_list = f.read()
                f.close()
                print('---------消费列表---------')
                for index in eval(shop_list):
                    print(index)
                print('用户还有 %s 的余额'%salary)
                f = open(money_path,'w')
                f.write(str(salary))
                f.close()
                rechoice = input('是否继续购买?  (Y/N) ').strip()
                if rechoice == 'Y'or rechoice == 'y':
                    continue
                else:
                    print('欢迎再次光临!')
                    flog = False
            else:
                print('无法识别该操作!')
    else:
        print("请重新输入用户名密码")
对昨日购物车程序进行了修整,并进行了小升级,感谢师兄的鼎力相助。

人在压力下才能不断进步,自由散漫的生活终究不行;抓紧时间往后面学习,购物车升级版的新内容很多,加快进度的同时也要保证质量
晚安

猜你喜欢

转载自www.cnblogs.com/ljy123/p/12596243.html