齐柳儿
为了梦想而奋斗
功能要求:
要求用户输入自己拥有总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]'''
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
while True:
    name =input("输入用户名:")
    mima =input("请输入用户密码:")
    if name =="qi"and mima =="123":
        print("登陆成功")
        break
    else:
        print("用户名或密码错误")
money = int(input("总资产"))
lst=[]
while 1:
    print('----------商品列表------------')
    for i,m in enumerate (goods):
        print(i,m)
    select =input("请选择您要购买的商品:")
    if select.isdigit():
        if 0<=int(select)<len(goods):
           s=goods[int(select)]
           a =s.get("price")
           print(a)
           if a >=money:
               print("账户余额不足")
           else:
               w = s.get("price")
               moneys =money-w
               print("余额还剩",moneys)
               lst.append(s)
               print(lst )
               print("购买成功")

        else:
            print('选择有误,请重新选择')

    elif  select == 'q':
        print('--------------选中商品列表-------------')
        for i,v in enumerate(lst):
            print(i,v)
        break

 

 
posted on 2018-08-07 17:08  齐柳儿  阅读(154)  评论(0编辑  收藏  举报