06python上

 1 product_list=[
 2     ('Mac',9000),
 3     ('kindle',800),
 4     ('tesla',900000),
 5     ('python book',105),
 6     ('bike',2000),
 7 
 8 ]
 9 saving=input('please input your money:')
10 shopping_car=[]
11 if saving.isdigit():   # 做出检验,检验输入是否为数字
12                        # isgraph检验是否为定义字符
13                        # isdigit检验是否为数字
14     saving=int(saving)    # int=()做出一个类型转换
15     while True:
16         #打印商品内容
17         for i,v in enumerate(product_list,1): #enumerate 在清单前面带出索引   加参数  1  表示编号从一开始
18             print(i,'>>>>',v)    #i,v   赋予给  索引和清单
19 
20          #引导用户选择商品
21         choice=input('选择购买商品编号[退出:q]:')
22 
23         #验证输入是否合法
24         if choice.isdigit():
25             choice=int(choice)
26             if choice>0 and choice<=len(product_list):
27                 #将用户选择商品通过choice取出来
28                 p_item=product_list[choice-1]
29 
30                 #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车
31                 if p_item[1]<saving:   #  [1]   表示调用索引【1】
32                     saving-=p_item[1]
33 
34                     shopping_car.append(p_item)   #将所选商品放入购物车。
35 
36                 else:
37                     print('余额不足,还剩%s'%saving)   #显示所剩余额
38                 print(p_item)
39             else:
40                 print('编码不存在')  #承接第二个 if 语句
41         elif choice=='q':
42             print('------------您已经购买如下商品----------------')
43             #循环遍历购物车里的商品,购物车存放的是已买商品
44             for i in shopping_car:
45                 print(i)
46             print('您还剩%s元钱'%saving)   #   %s格式化输出
47             break                         #  跳出循环
48         else:
49             print('invalid input')

2018-08-08  

   最次的      购物车

#_author:supreme
#time:2018/8/8,9:54
#a=[[1,2,],"meony",4,(2,3,4)]
#print(a[0][1])


product_list=[
    (" Mac Book",12000),
    ("自行车",1000),
    ("饮水机",1500),
    ("茶几",2000),
    ("小米电视",4000),
    ("小米盒子",200),
    ("云米电冰箱",4000),


]
saving=input("输入您现有的金额:")
shopping_car=[]
if saving.isdigit():
    saving=int(saving)
    while True:
        for i,v in enumerate(product_list,1):
            print(i,"<<<<",v)
        choice=input("选择购买商品编号[退出:q]:")
        if choice.isdigit():
            choice = int(choice)
            if choice > 0 and choice <= len(product_list):
                p_item=product_list[choice-1]
                if p_item[1] < saving:
                    saving -= p_item[1]
                    shopping_car.append(p_item)
                else:
                    print("余额不足,还剩%s" %saving)
                print(p_item)
            else:
                print("编码不存在")
        elif choice == "q":
            print("-------您已经购买如下商品--------")
            for i in shopping_car:
                print(i)
            print("您还剩%s元钱" % saving)
            break
        else:
            print('invalid input')

删除:   a.remove()

      a.pop()   #    ()内容为数字 ,牵引的编号

    del       #可以删除任何值、表、对象。

计算  count

添加  extend

附加  append

len(product_list)  显示列表长度

原文地址:https://www.cnblogs.com/money131/p/9445600.html