Python(41)_高階一點的購物車程式
阿新 • • 發佈:2019-01-02
#-*-coding:utf-8-*- ''' 輸出商品列表 ''' li =[{'name':'蘋果','price':10}, {'name':'香蕉','price':8}, {'name':'西瓜','price':3} ] #print(li[0]) # {'name': '蘋果', 'price': 10} #print(li[0]['name']) # 蘋果 #print(li[0]['price']) #rint(type(li[0]['price'])) shoping_car = {} # print("歡迎光臨") money = input("讓我看看你的錢:") if money.isdigit() and int(money)>0: # 有錢就給它展示商品 money = int(money) for i,k in enumerate(li): print('序號{},商品 {},價格 {}'.format(i+1,k['name'],k['price'])) choose = input("請輸入您要購買的商品序號:") if choose.isdigit() and int(choose) <=len(li): num = input('請輸入您要購買的數量:') if num.isdigit(): if int(money)>li[int(choose)]['price']*int(num): money = money - li[int(choose)]['price']*int(num) if li[int(choose)]['name'] in shoping_car: shoping_car[li[int(choose)]['name']] = shoping_car[li[int(choose)]['name']] + int(num) else: shoping_car[li[int(choose)]['name']] = int(num) print("購物車的商品有{},您的餘額{}".format(shoping_car,money)) else: print("窮鬼") else: print("都說了是序號,你傻啊")