1. 程式人生 > >Python 學習 day4

Python 學習 day4

購物車程式的詳解

shop = [
	['iphone8',5000],
	['kndle',988],
	['ps4 pro',2800],
	['psv',1200],
	['mp3',100]
]
shop_car = []
saving = input("請輸入預算:")
# 驗證使用者輸入是否正確
if saving.isdigit():
	saving=int(saving)
while True:
# 列出商品列表
	for i,v in enumerate(shop,1):
		print(i,"-",v)
		# 讓使用者選擇商品到購物車
	choice = input("請選擇商品編碼到購物車:[確認:q]"
) if choice.isdigit(): choice=int(choice) if choice > 0 and choice<=len(shop): p_item = shop[choice-1] if p_item[1]<saving: saving -= p_item[1] shop_car.append(p_item) print("您選購的商品%s,剩餘%s" % (p_item,saving)) else: print("您的餘額不足,剩餘%s" % saving) # 讓客戶確認購物車內購買的商品
elif choice=="q": print("----您的購物車清單----") for i in shop_car: print(i) # 讓客戶確認是否購買並列印購物車和餘額 guess = input("是否確認購買:q/n") if guess == "q": print("您已經購買%s,餘額%s" % (shop_car,saving)) break # 客戶沒有確認退出 else: print("歡迎再來") break else: print("輸入編碼無效") else: print('輸入金錢無效')

字典

  • 字典是python中唯一的對映型別,採用鍵值對(key-value)的形式儲存資料。python對key進行雜湊函式運算,根據計算的結果決定value的儲存地址,所以無序儲存的且key必須是可雜湊的。可雜湊表示不可變型別如:數字、字串、元祖
    • 不可變型別:整型,字串,元祖
    • 可變型別:列表,字典
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
print(dict1["name"]
# 這裡會報錯,key值不能是可雜湊的值
dict1 = {[1,2]:"alex","age":29,"hobby":"game","is_handsome":True}
# 不報錯,value值可以隨意
dict1 = {"name":["alex",29],"age":29,"hobby":"game","is_handsome":True}
dict2 = dict([["name","alex"]])

字典的操作

dict1 = {'name':"alex"}
dict1["age"]=18
print(dict1)
# 如果鍵值存在不改變,返回值是原值
rst =dict1.setdefault("age","book")
print(rst) # 18 
# 鍵不存在,增加新的鍵值,返回值是相應的值
rst1 = dict1.setdefault("hobby","book")
print(rst1) # book
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
print(dict1["name"])
print(list(dict1.keys()))
print(list(dict1.values()))
print(list(dict1.items()))
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
dict1["name"]=29
dict2 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
dict3 = {"1":"111","2":"222","3":"333"}
dict2.update(dict3)
dict2 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
# 清空字典
dict2.clear()
print(dic2)
# del 刪除字典中指定鍵值對
del dict2["name"]
# pop 刪除字典中指定鍵值對返回該鍵值對的值
print(dict2.pop("age"))
# popitem 隨機刪除某組鍵值對,並以元祖方式返回
a = dict2.popitem()
print(a,dict2)
  • 其他操作方法
dic6 = dict.fromkeys(["host1","host2","host3","host4"],"test")
  • 字典迴圈遍歷
dic5 = {"name":"alex","age":18,"hobby":"python"}
for i in dic5:  # 推薦這種
	print(i,dic5[i])
for i,v for dic5.items(): # 這樣佔記憶體大不推薦使用
	print(i,v)
  • 字串操作
#1. 重複輸出字串
a = ("hello" * 2)
print(a)
# 2. 通過索引
a = ("helloworld"[2:])
print(a)
# 3. 關鍵 in 判斷是否在字串內,如果在返回True
print(123 in "123456hello"# 4. 格式化字串
print("alex is a good teacher")
print("%s is a good teacher" % "alex")
# 5. 字串的拼接
a = "123"
b = "abc"
c = "444"
# .join方法拼接
d = "-----".join([a,b,c])
print(d)
  • 字串的內建方法:
st = "hello kitty{name} is {age}"
print(st.count("l")) # 統計元素個數
print(st.capitalize()) # 首字母大寫
print(st.center(50,"-")) # 居中元素
print(st.casefold()# 全部大寫 用不到
print(st.encode()) # 解碼
print(st.endswitch("tty3")) # 判斷是否以某個內容結尾
print(st.startswitch("he")) # 判斷是否以某個內容開頭
print(st.expandtabs(tabsize=10)) # 轉換\t 幾個空格 用不到
print(st.find("t")) # 查詢到第一個元素,並將索引值返回
print(st.format(name = "Alex",age = 37)) # 格式化輸出的另一個版本
print(st.format_map({"name":"alex","age":22}) # 格式化輸出的字典版本
print(st.index("t")) # 與find的區別就是報錯
print("abc456".isalnum()) # 判斷是否是字母和數字 用不到
print("1231234".isdeciaml()) # 判斷是否為十進位制 用不到
print("126323178".isdigit()) # 判斷是否為整型數字
print("abc".isidentifier()) # 判斷是否非法變數
print("abc".islower()) # 判斷是否為小寫
print("ABC".isupper()) # 判斷是否為大寫.
print("  ".isspace()) # 判斷是否為空格
print("My Tittle".istittle()) # 判斷是否為標題
print("My Tittle".lower()) # 所有字元變小寫
print("My Tittle".upper())  # 所有字元變大寫
print("My Tittle".swapcase())  # 大小寫反轉
print("My Tittle".ljust(50,"*")) # 向左新增字串
print("My Tittle".rjust(50,"*")) # 向右新增字串
print("\tMy Tittle\n".lstrip()) # 去掉左邊的換行符
print("\tMy Tittle\n".rstrip()) # 去掉右邊的換行符
print("\tMy Tittle\n".strip()) # 去掉所有的換行符
print("My Tittle\n".replace("Tittle","lesson")) # 替換字元
print("My Tittle".split(" ")) # 以左分割成列表
print("My Tittle".rsplit(" "))  # 向右分割成列表
print("My Tittle tittle".title()) # 把所有字母首開頭變字母
  • 重要的字串方法
print(st.count("l")) # 統計元素個數
print(st.startswitch("he")) # 判斷是否以某個內容開頭
print(st.center(50,"-")) # 居中元素
print(st.find("t")) # 查詢到第一個元素,並將索引值返回
print(st.format(name = "Alex",age = 37)) # 格式化輸出的另一個版本
print("My Tittle".lower()) # 所有字元變小寫
print("My Tittle".upper())  # 所有字元變大寫
print("\tMy Tittle\n".strip()) # 去掉所有的換行符 ******
print("My Tittle\n".replace("Tittle","lesson")) # 替換字元
print("My Tittle".split(" ")) # 以左分割成列表

作業:

  • 程式:三級選單
  • 要求
  1. 列印省、市、縣三級選單
  2. 可返回上一級
  3. 可隨時退出程式