5月31日作业, 字典 取值,分别赋值等

1. 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}

-----------------------------------------------

  shopping_car=[]
# while True:
# for k in msg:
# info=('商品:%s 价钱:%s'%(k,msg[k]))
# print(info.center(50," "))
#
# name=input('please input shopping name').strip()
# if name not in msg:
# print('重现输入')
# while True :
# count=input('选择商品数量').strip()
# if count. isdigit:
# coun=int(count)
# break
# else:
# print('input ')
# for item in shopping_car:
# item["count"]+=count
# break
# else:
# price=msg[name]
# info={'name':name,'count':count,'price':price}
# shopping_car.append(info)
# print(shopping_car)
------------------------------------------------------------

2. 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中


# for i in a:
# if i<66:
# c['b'].append(i)
# else:
# c['g'].append(i)
# print(c)
#
# c={'key1':[],'key2':[]}
# for i in a :
# if i <66:
# c['key1'].append(i)
# else:
# c['key2'].append(i)
# print(c)

 ------------------------------------------------------------------------------------

3. 统计s='hello alex alex say hello sb sb'中每个单词的个数
结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}

s='hello alex alex say hello sb sb'


l=s.split()
dic={}
for item in l:
    if item in dic:
        dic[item]+=1
    else:
        dic[item]=1
print(dic)

4. 有如下列表,请采用两种方式取出列表中的值
 my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
 方式一:依赖索引,请写出while循环与for循环两种实现方式
 方式二:不依赖索引

my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
#
# for i in my_girl_friends:
# print(i)
#
# i=0
# while i<len(my_girl_friends):
# print(my_girl_friends[i])
# i+=1

猜你喜欢

转载自www.cnblogs.com/lijieshi/p/9119360.html
今日推荐