python基础 day6

dict: dic = {'name':'alex'}
增:dic['age'] = 21 存在就覆盖
dic.setdefault() 没有就增加

删除:pop()按照key删除,有返回值
clear
del dic['name']
popitem()随机删除 返回的是元祖

改 update


dic.keys()
dic.values()
dic.items()

for k,v in dic.items():
print(k,v)

dic.get(key,None)

while 1:
    li=["手机",'电脑','鼠标垫','游艇']
    for i in li:
        print('{}\t\t{}'.format(li.index(i)+1,i))
    num_of_choice=input("请输入商品的序号,按'Q'或'q'退出:")
    if num_of_choice.isdigit():
        num_of_choice=int(num_of_choice)
        if num_of_choice>0 and num_of_choice<=len(li):
            print(li[num_of_choice-1])

        else:print('请输入有效数字!')
    elif num_of_choice.upper()=='Q':
        break
    else:
        print('输入错误,请重新输入!')

 

 编码:

ascii
A: 00000010 8位 一个字符


unicode
A: 00000000 00000000 000000010 00000010 32位 四个字符
中:

utf-8
A: 00001000 8位 一个字符
中:00000010 00000000 00100000 24位 三个字符


gbk
A: 00001000 8位 一个字符
中: 00001000 00100000 16位 二个字符

各个编码之间的二进制,是不能互相识别的,会产生乱码


#encode 编码 将str---->bytes
# s1='alex'
# s11=s1.encode('utf-8')
# print(s1)
s2='中国'
s22=s2.encode('utf-8')
print(s22)
#购物车
li=[
    {'name':'苹果','price':10},
    {'name':'香蕉','price':20},
    {'name':'西瓜','price':30}
]
shopping_car={}
#把货物放在货架上
print('欢迎光临大铁锤水果店!')
money=input('让我看看你的钱')
shopping_car={}
if money.isdigit() and int(money)>0:
    while 1:
        for i,k in enumerate(li):
            print('序号:{}\t商品:{}\t价格:{}'.format(i,k['name'],k['price']))
        choose = input('请输入您要购买的商品序号,按Q退出')
        if choose.isdigit() and int(choose)<len(li):
            num=input('请输入你要购买的数量')
            if int(money)>=li[int(choose)]['price']*int(num):
                money=int(money)-li[int(choose)]['price']*int(num)
                if li[int(choose)]['name'] in shopping_car:
                    shopping_car[li[int(choose)]['name']]=shopping_car[li[int(choose)]['name']]+int(num)
                else:
                    shopping_car[li[int(choose)]['name']]=int(num)
                print('您选择的商品有{}\t{}'.format(shopping_car, money))

            else:
                print('穷鬼,回去跟你老婆要钱去!')
                break
        elif choose.upper() == 'Q':
            print('谢谢你的惠顾,欢迎下次光临!')
            break
        else:
            print('都说了是序号,你傻啊!')
购物车

 


 

 

 

 

 

posted @ 2019-03-15 22:34  遗忘天际  阅读(124)  评论(0编辑  收藏  举报