python第九天作业

第一题

  有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量

info = ['alex', 49, [1900, 3, 18]]
name, age, birth = info
birth_year, birth_month, birth_day = birth
data = [name, age, birth_year, birth_day, birth_day]
key = ['名字:', '年龄:', '出生年:', '月:', '日:']
i = 0
while i < 5:
    print(key[i], data[i], end=' ')
    i += 1

第二题

  用列表的insert与pop方法模拟队列

l = []
l.insert(len(l), 1)
l.insert(len(l), 2)
l.insert(len(l), 3)
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

第三题

  用列表的insert与pop方法模拟堆栈

l = []
l.insert(0, 1)
l.insert(0, 2)
l.insert(0, 3)
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

第四题

购物车,要求如下:

  实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic = {
    'apple': 10,
    'tesla': 100000,
    'mac': 3000,
    'lenovo': 30000,
    'chicken': 10,
}
l = []
while True:
    access_name = input('请输入正确的商品名称(输入q或Q退出):')
    if access_name == 'q' or access_name == 'Q':
        print('欢迎下次光临')
        break
    access_number = input('请输入购买个数:')
    if access_name in msg_dic and access_number.isdigit():
        number = int(access_number)
        name = access_name
        value = msg_dic[name] * number
        t = (name, number, value)
        l.append(t)
        print(l)
    else:
        print('格式输入错误,请重新输入')

第五题

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

l = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
d = {'k1':[],'k2':[]}
for i in l:
    if i > 66:
        d['k1'].append(i)
    else:
        d['k2'].append(i)
print(d)

第六题

  统计s='hello alex alex say hello sb sb'中每个单词的个数

s='hello alex alex say hello sb sb'
s_list = s.split()
dic = {}
for i in s_list:
    dic[i] = s_list.count(i)
print(dic)
原文地址:https://www.cnblogs.com/Lance-WJ/p/12464584.html