day06-1 數據結構-購物車
阿新 • • 發佈:2018-02-10
pin 數據 inpu 不存在 價格 auth python 引導 while
#__author: hasee #date: 2018/2/4 product_list=[ (‘Mac‘,9000), (‘kindle‘,800), (‘tesla‘,900000), (‘python book‘,105), (‘bike‘,2000), ] saving=input(‘please input your money:‘) shopping_car=[] if saving.isdigit(): #判斷是不是一個數字 saving=int(saving) while True: #打印商品內容 for i,v in enumerate(product_list,1): #enumerate統計序列,參數後1,表示從1開始統計 print(i,‘>>>>‘,v) #引導用戶選擇商品 choice=input(‘選擇購買商品編號[退出:q]:‘) #驗證輸入是否合法 if choice.isdigit(): choice=int(choice) if choice>0 and choice<=len(product_list): #將用戶選擇商品通過choice取出來 p_item=product_list[choice-1] #如果錢夠,用本金saving減去該商品價格,並將該商品加入購物車 if p_item[1]<saving: saving-=p_item[1] shopping_car.append(p_item) #append 追加進去 else: print(‘余額不足,還剩%s‘%saving) print(p_item) else: print(‘編碼不存在‘) elif choice==‘q‘: print(‘------------您已經購買如下商品----------------‘) #循環遍歷購物車裏的商品,購物車存放的是已買商品 for i in shopping_car: print(i) print(‘您還剩%s元錢‘%saving) break else: print(‘invalid input‘)
day06-1 數據結構-購物車