學習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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279781.html
標籤:其他
上一篇:GinAdmin——基于golang的web管理平臺
下一篇:創建了一個Django專案,但是運行時報錯 Error: [WinError 10013] 以一種訪問權限不允許的方式做了一個訪問套接字的嘗試。
