.clear()# 清空列表 del name #删除列表
name.extend(name2) #把列表2加入到name里
.reverse() #反转列表
.sort() # 排序 相同类型
非列表知识补充:choice.isdigit() #判断字符是否为数字
更改代码颜色: \033[031;1m \033[0m
key是唯一的, 字典是无序的
删 : name.pop(key) 删除指定key
name.popitem() 随机删
查 : name.get() 没有则返回none
113 in name 判断字典里有没有113这个key
for k,v in name.items( ): 不建议用,效率太低
print (k,v)
name.update(字典) 更新字典
shopping_cart = [] product_list = [ [‘iphone‘,5800], [‘book‘,30], [‘茅台酒‘,650], [‘笔‘,5], [‘小米手机‘,2000], ] salary = int(input(‘input you salary:‘)) while True: index = 0 for product in product_list: print(index,product) index+=1 choice = input(‘请输入商品编号,或者输入q退出:‘).strip() if choice.isdigit(): #判断输入是否为数字 choice = int(choice) if choice >=0 and choice <= len(product_list): #商品存在 product = product_list[choice] if product[1] <= salary: #买得起 shopping_cart.append(product) salary-=product[1] print(‘Added product ‘+product[0]+‘ into shopping_cart ,your current balance:‘+str(salary)) else: print(‘工资不够,商品的价格是‘+str(product[1])+‘你还差‘+str(product[1]-salary)) else: print(‘商品不存在‘) elif choice==‘q‘: print(‘\033[31;1m------------已购买商品列表--------------\033[0m‘) for i in shopping_cart: print(i) print(‘您的余额为:‘,salary) print(‘\033[31;1m----------------end--------------------\033[0m‘) break else: print(‘无此选项‘)
menu = { ‘北京‘:{ ‘海淀‘:{ ‘五道口‘:{ ‘soho‘:{}, ‘网易‘:{}, ‘google‘:{} }, ‘中关村‘:{ ‘爱奇艺‘:{}, ‘汽车之家‘:{}, ‘youku‘:{}, }, ‘上地‘:{ ‘百度‘:{}, }, }, ‘昌平‘:{ ‘沙河‘:{ ‘老男孩‘:{}, ‘北航‘:{}, }, ‘天通苑‘:{}, ‘回龙观‘:{}, }, ‘朝阳‘:{}, ‘东城‘:{}, }, ‘上海‘:{ ‘闵行‘:{ "人民广场":{ ‘炸鸡店‘:{} } }, ‘闸北‘:{ ‘火车战‘:{ ‘携程‘:{} } }, ‘浦东‘:{}, }, ‘山东‘:{}, } break_flag=True for k in menu.keys(): print(k) #打印第一级菜单 while break_flag: choice1 = input(‘请输入目的地:‘) #输入第一级菜单 if choice1 in menu.keys(): #输入字符在第一级菜单里 for i in menu[choice1].keys(): #遍历第二级菜单 print(i) #打印第二级菜单 choice2 = input(‘请输入目的地:‘) #输入第二级菜单 if choice2 in menu[choice1].keys(): #输入字符在第二级菜单里 for j in menu[choice1][choice2].keys(): #遍历第三级菜单 print(j) #打印第三级菜单 choice3 = input(‘请输入目的地:‘) #输入第三级菜单 if choice3 in menu[choice1][choice2].keys(): #输入字符在第三级菜单里 for m in menu[choice1][choice2][choice3].keys(): #遍历第四级菜单 print(m) #输出第四季菜单 choice4 = input(‘请选择b返回,或者q直接退出‘) if choice4 ==‘q‘: break_flag = False break elif choice4==‘b‘: break else: print(‘请输入正确字符‘) elif choice3 == ‘q‘: break_flag = False break elif choice3 == ‘b‘: break else: print(‘请输入正确字符‘) elif choice2 == ‘q‘: break_flag = False break elif choice2 == ‘b‘: break else: print(‘请输入正确字符‘) elif choice1 == ‘q‘: break_flag = False break elif choice1 == ‘b‘: break else: print(‘请输入正确字符‘)
原文:http://www.cnblogs.com/guotianbao/p/6715514.html