python学习笔记 day06

作业回顾

1.把列表中大于66的元素放在字典中键K1中,把列表中小于66的元素放在字典中键K2中

L=[12,33,56,34,19,78,45,90,87,86,95,15]
dic={}
value_1,value_2=[],[]
for i in L:
    if i>66:
        value_1.append(i)
        dic['k1']=value_1
    else:
        value_2.append(i)
        dic['k2']=value_2
print(dic)

运行结果:

F:\workspace_python\pycharm_projects\venv\Scripts\python.exe F:/workspace_python/pycharm_projects/fullstack2018-08-17/week2/day07/01.py
{'k2': [12, 33, 56, 34, 19, 45, 15], 'k1': [78, 90, 87, 86, 95]}

2.输出商品列表,用户输入序号,显示所选的商品:

显示商品序号和对应的商品,根据用户输入的序号,展示所代表的商品;

如果用户输入商品号不对,则报错;

如果用户输入Q(q)则退出;

L=['computer','cell phone','mouse','ship']
for i in range(len(L)):
    print(i+1,L[i])
while 1:
    num=input(">>>")
    if num.lower()=='q':
        print('End')
        break
    if int(num)>len(L):
        print("Error,try again")
    else:
        print(L[int(num)-1])

 

posted @ 2018-08-21 19:06  写的BUG代码少  阅读(147)  评论(0编辑  收藏  举报