字典需要掌握知识点:
1、两个特点:
- 无序的
- 没有下标
2、常用的方法
查、增加、修改
info[key] = "value"
删除
- del info[key]
- info.pop("key") 常用
- info.popitem() 随机删除
查找
info.get("key") 安全的查找方式
判断在不在字典中
print("key" in info)
info.has_key("key") python2中使用
嵌套:key最好不要写中文,避免编码问题找不到
循环:
for i in info:
print(i,info[i])
for k,v in info.items(): 需转成列表,数据量大的时候效率低下
print(k,v)
setDefault() 先查找,有就返回,没有就创建
info.update(b) 将两个字典合并,有相同key的就更新成b中的value
info.items()字典转换成列表
fromkeys 初始化字典
dict.fromkeys([6,7,8],"test)
注意多层value,改一个值,全部更改(引用的同一块地址)
购物车程序升级版(day02作业)
自己写的,实现功能为目的
之前的购物车功能都要有,并且增加以下需求:
- 用户入口:
1、商品信息存在文件里
2、已购商品,余额记录(都存在文件里了) - 商家入口:
可以添加商品,修改价格(只做了修改价格,写博客的时候才发现少做,歪日)
先是两个文件:
shopping_list.txt 存放商品信息
shopping_record.txt 存放购买记录和余额记录
用户和商家写了两个python文件,别问我为什么,懒:
用户入口:和上个版本的几户一样,只是增加了判断用户是否第一次登陆,增加了个判断,我设置的是判断购买记录文件是否为空。
#购物车
#用户入口:
#1、商品信息存在文件里
#2、已购商品,余额记录
#商家入口:
#可以添加商品,修改价格
import os
f = open("shopping_list.txt")
f2 = open("shopping_record.txt","r+")
shopping_list = [] #从文件中获取的商品列表
shopping_cart = [] #已购商品清单
#从文件中读取,生成商品列表
for line in f:
str_line = line.strip()
shopping_list.append(str_line.split(" "))
# print(shopping_list[0][1])
#判断是否第一次进入,是就让用户输入余额,否则从文件(shopping_record.txt)中获取余额
if os.path.getsize("shopping_record.txt") == 0:
salary = input("请输入余额:")
else:
# print("salary is")
# print(os.path.getsize("shopping_record.txt"))
for line2 in f2:
str_line2 = line2.strip()
salary = str_line2[str_line2.index("-")+1:]
print("您已有购买记录,余额%s"%salary)
#用户购买商品,按q退出
if salary.isdigit():
salary = int(salary)
while True:
print(shopping_list)
user_choice = input("请选择是否购买:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice >=0 and user_choice < len(shopping_list):
if salary >= int(shopping_list[user_choice][1]):
shopping_cart.append(shopping_list[user_choice])
salary -= int(shopping_list[user_choice][1])
print("已将%s添加购物车,余额还剩%s " % (shopping_list[user_choice], salary))
else:
print("余额只剩%s,还买个屁啊" % salary)
else:
print("没有此商品")
elif user_choice =="q":
print("-------------you buy-----------")
for p in shopping_cart:
print(p)
#将购买记录写入文件shopping_record.txt
if len(shopping_cart)!=0:
str_write = str(shopping_cart)+"-"+str(salary)
f2.write(str_write+'\n')
f.close()
f2.close()
print("your current balance is %s" %salary)
exit()
#退出打印
商家入口
只写了修改商品单价,忘了增加商品这一功能了
#商家入口
f = open("shopping_list.txt")
choice = input("选择更改价格的商品:")
shopping_list = []
if choice.isdigit():
choice = int(choice)
#读取文件,生成商品列表
for line in f:
shopping_list.append(line)
f.close()
# print(shopping_list)
# print(shopping_list[choice])
#选择要更改的商品
str_choice = shopping_list[choice]
new = input("请输入新的价格:")
if new.isdigit():
new = int(new)
#将更改后的商品字符串重新赋给列表
shopping_list[choice]=str_choice[:str_choice.index(" ")]+" "+str(new)+"\n"
print("您已修改成功,新的价格为:",shopping_list[choice])
#将新的商品列表写入原来的文件
f = open("shopping_list.txt","w")
for i in shopping_list:
f.write(i)
f.close()
shopping_record.txt:
[['macbookpro', '10000']]-10000
[['apple', '20']]-9980
[['bike', '200'], ['iphone', '5000']]-4780
[['bike', '200']]-4580
shopping_list.txt:
开始自行车是200块,测试改成2000了
macbookpro 10000
bike 2000
apple 20
iphone 5000