学习Python3,坚持每一次学习都有一点点知识的积累,瞄准目标gogogo!这次仍然是练习,增加字符串操作、dict字典的合理使用,使用了一些稍微复杂的逻辑(题目源自老男孩)
购物车程序商家入口:
可以增加商品;
修改商品。
先上代码:
#Author wsp ##商家程序,要求如下: #可以添加商品,修改商品价格 print("这里是商品后台系统".center(70, "#")) while True: #只读方式打开文件 goods_file_r = open("goods.txt", "r") lines = goods_file_r.readlines() #涉及到去重,使用字典 goods_list = {} print("当前商品列表如下:") for line in lines: goods_line = line.split(" ") if goods_line.__len__() == 2: goods_list[goods_line[0].strip()] = goods_line[1].strip() print("{name} {cost}".format(name=goods_line[0].strip(), cost=goods_line[1].strip())) #关闭文件句柄 goods_file_r.close() choice = input("商品信息显示完成,请选择操作,1-》增加商品;2-》修改商品:") if choice.isdigit(): choice = int(choice) ##增加商品 if choice == 1: new_goods_name = input("请输入要增加的商品名:") if len(new_goods_name) != 0 and not goods_list.__contains__("new_goods_name"): while True: new_goods_cost = input("请输入商品价格:") if new_goods_cost.isdigit(): new_goods_cost = int(new_goods_cost) goods_list[new_goods_name] = new_goods_cost #写的方式打开文件 goods_file_w = open("goods.txt", "w") for goods_new in goods_list: line = "{name} {cost}\n".format(name=goods_new, cost=goods_list[goods_new]) goods_file_w.write(line) #1goods_file.write('\n') print("增加商品{name},金额为{cost}".format(name=new_goods_name, cost=new_goods_cost)) #关闭写文件句柄 goods_file_w.close() break else: print("请输入正确的价格") continue else: print("请输入正确的商品名称") #修改商品 elif choice == 2: new_goods_name = input("请输入要修改的商品名:") if goods_list.__contains__(new_goods_name): while True: new_goods_cost = input("请输入商品价格:") if new_goods_cost.isdigit(): new_goods_cost = int(new_goods_cost) goods_list[new_goods_name] = new_goods_cost # 写的方式打开文件 goods_file_w = open("goods.txt", "w") for goods_new in goods_list: goods_file_w.writelines("{name} {cost}".format(name=goods_new, cost=goods_list[goods_new])) goods_file_w.write('\n') goods_file_w.close() break else: print("请输入正确的价格") continue else: print("商品{new_goods_name}不存在请输入正确的商品名称".format(new_goods_name=new_goods_name)) else: print("请输入正确的数字!")
要点(今天有点事明天更新要点。。。。。):
string相关的使用方法:
#Author wsp name = "wang wang peng" print(name.capitalize()) print(name.center(50, "-")) print(name.encode()) print(name.endswith("peng")) print(name.expandtabs(tabsize=30)) print(name.find("wang"))#找到对应的索引 print(name.isalnum()) print(name.isalpha()) print("1a212".isdecimal())#判定是否是十进制 print("1A".isidentifier())#判断是否是合法的标示符,也就是能否当做变量名 print("a".islower())#是否小写 print("1212".isnumeric())#是否只含有数字 print(" ".isspace())#是否是空格 print('i like you!'.join("==")) print("+".join(['1','2','3'])) print(name.ljust(50,"*"))#右侧补充 print(name.rjust(50,"*"))#左侧补充 print("AAAA".lower()) print("asdasd".lstrip())#去掉左边的空格或者回车 p = str.maketrans("abcdef","123456") print("i like you ".translate(p)) print("asdsadasd".replace("a","A",1)) print("asdsadasd".rfind("d"))#从右开始找,第一个d的位置 print("i like you ".split("i"))#分割字符串,i为分隔符 print("I like you".swapcase())#大写变小写
dict字典:
#Author wsp #字典相关 #字典是无序的 #key是唯一的,天生去重 #定义: name_map = { "james": "123456", "ad": "123123" } print(name_map) name_map["james"] print(name_map) del name_map["ad"] name_map.pop("james") #name_map.popitem() name_map.get("ad")#此方法比较简单,如果没有在字典里,也不会报错 "ad" in name_map #判定是否有ad存在 name_map1 = { "james": "123456", "ad": "123123" } print(name_map1.items()) print(dict.fromkeys([1,2,3],"keys")) #有个坑 ff= dict.fromkeys([1, 2, 3], [1, {"name": "alex"}, 444]) print(ff) ff[2][1]['name']= 'jack' print(ff)