1. 程式人生 > >Python3.6全棧開發實例[012]

Python3.6全棧開發實例[012]

pen 價格 while 重新 用戶輸入 choice code bre 打印

12.輸出商品列表,用戶輸入序號,顯示用戶選中的商品(升級題)
商品列表:
goods = [{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998}, ]
要求:
1:頁面顯示 序號 + 商品名稱 + 商品價格,如:
1 電腦 1999
2 鼠標 10

2:用戶輸入選擇的商品序號,然後打印商品名稱及商品價格
3:如果用戶輸入的商品序號有誤,則提示輸入有誤,並重新輸入。
4:用戶輸入Q或者q,退出程序。

lst = []
shopcars = {}
while 1:
    goods = [{"name": "電腦", "price": 1999},
     {"name": "鼠標", "price": 10},
     {"name": "遊艇", "price": 20},
     {"name": "美女", "price": 998}, ]
    for i,j in enumerate(goods,1):
        print(i,j[name],j[price])
        lst.append(j)
    choice = input(請輸入商品編號選擇商品:
) if choice.isdigit(): choice = int(choice) pname = lst[choice-1][name] pprice = lst[choice-1][price] print(pname,pprice) if pname in shopcars: shopcars[pname][amount] +=1 else: shopcars[pname] = {pprice: pprice, amount
: 1} elif choice.upper() == Q: print(shopcars) break else: print(商品序號有誤!)

Python3.6全棧開發實例[012]