python第八天作业代码

'''
PY S6
YRY
'''
# # 作业一:打印99乘法表
for i in range(1, 10):
for j in range(1, 10):
if i >= j:
print('%s X %s=' % (j, i), i*j, end=' ')
print(' ')
# # 作业二:简单购物车,实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
msg_dic = {
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
user_goods = []
while True:
for item, value in msg_dic.items():
print('商品:{name},价格:{value}'.format(name=item, value=value))
select = input('选择商品:').strip()
if len(select) == 0 or select not in msg_dic.keys():continue
num = input('商品数量:').strip()
if num == 0 or not num.isdigit():
print('请输入大于0的整数')
continue
num = int(num)
user_goods.append([select, msg_dic[select], num])
print(user_goods)


# 作业三:字典练习1 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。(2分)
#  即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

l = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
dict_l = {'key1': [], 'key2': []}
for i in l:
if i > 66:
dict_l['key1'].append(i)
if i < 66:
dict_l['key2'].append(i)
print(dict_l)

#   2 统计s='hello alex alex say hello sb sb'中每个单词的个数
#   结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
s = 'hello alex alex say hello sb sb'
d = {}.fromkeys(list(s.split()), None)
for i in d:
d[i] = s.count(i)
print(d)
原文地址:https://www.cnblogs.com/fenglin0826/p/7211987.html