拿你有的,换你要的;这个世界一直如此,很残酷,却很公平!

day06-回归列表-元组-购物车-字典

回顾列表[]

  • 增(append,insert)
  • 删(remove,del,pop)   
  • 改(重新赋值) 
  • 查([0,1])

count(‘ ’): 统计某个元素在列表中出现的次数

extend(‘[] ’):在列表末尾一次性追加另一个序列中的多个值。 + 不会修改原来的列表

index(‘’):用于列表中找出某个值第一个匹配项的索引值。

sort([]):永久排序

sorted([]):排序不改变原来列表

 

元组:只读列表,无法修改  元组符号:()

#Time : 2020/6/8 16:29
#Author : wb
#FileName: day06-shopping.py
#Email : 945784220@qq.com
#Software: PyCharm
#Blog:https://www.cnblogs.com/BBS2013/
import numpy as np
product_list=[
    ('one',1),
    ('two',2),
    ('three',3),
    ('four',4)
]
shopping_car=[]
saving=input('please input your money:')
if saving.isdigit():
    saving=int(saving)
    while True:
        #打印商品内容
        for i,v in enumerate(product_list,1):          # enumerate(sequence, [start=0])  组合为一个索引序列,同时列出数据和数据下标
            print(i,'>>>>>',v)
            #print(product_list.index(i),i)     #  index获取索引值,也就是序号的意思
            #引导用户选择商品
            choice = input('选择购买商品编号[退出:q]:')
            #输入是否合法
            if choice.isdigit() :
                choice=int(choice)
                #判断是否在序列中,将用户商品通过‘choice’取出
                if choice >= 0 and choice <= len(product_list):
                    P_iterm=product_list[choice-1]
                    #判断用户钱包是否充足,用本金saving减去该商品价格,并将商品加入购物车
                    if P_iterm[1]<saving:
                        saving-=P_iterm[1]
                        shopping_car.append(P_iterm[0])
                        print(P_iterm)
                    else:
                        print('余额不足,还剩%s' %saving)
                else:
                    print('编码不存在')

            elif choice == 'q' :
                print('---您购买了如下商品---')
                #循环遍历购物车商品,购物车需去重,直接改成量
                for i in shopping_car:
                    #  去重参考:https://blog.csdn.net/weixin_45342712/article/details/94615956
                                    #                 dic={}.fromkeys(shopping_car)
                                    #                 if len(dic) == len(shopping_car):
                                    #                     #print('列表里的元素互不重复!')
                                    #                     print(i)
                                    #                 else:
                                    #                     print(dic)  #如何确定重复数量?????
                    #lst = [1, 3, 5, 8, 9, 9, 0, 0, 3, 3]
                    new_list = []

                    for i in product_list:
                        if i not in new_list:
                            new_list.append(i)
                        else:
                            ???




                    print(i)
                    print('您的余额为%s' %saving)
                break
            else:
                print('invalid input')
else:
    print('invalid input')

 

posted @ 2020-06-08 16:36  bowuwb  阅读(142)  评论(0编辑  收藏  举报
Fork me on GitHub