標籤:blog 沒有 break bin class one ota please append
1,購物車小程式
需求:
(1),啟動程式後,列印全部商品列表,使用者輸入工資
(2),許使用者根據商品編號購買商品
(3),使用者購買商品,餘額不足就退出程式,並列印資訊
(4),可隨時退出,退出時,列印已購買商品和餘額
2,流程圖
3,代碼
#!/usr/bin/python3Product_list = [ (‘Doido鑽戒 ‘,8000), (‘ROLEX手錶‘,20000), (‘HuaWei P10‘,4000), (‘AppleWatch‘,2000), (‘Ipad‘,1000), (‘Asics‘,500)]Shopping_list = []for i in enumerate(Product_list): print(i)TotalSalary = input(‘Your salary oror (q to quit process):‘)while True: AvailableProduct = [] if TotalSalary == ‘q‘: print(‘什麼也沒有買!!‘) break for i in range(len(Product_list)): if int(TotalSalary) >= Product_list[i][1]: AvailableProduct.append(Product_list[i]) if len(AvailableProduct): for i in enumerate(AvailableProduct): print(i) UserChoice = input("Please enter product‘s code that you want to buy(or q to quit):") if not UserChoice.isdigit(): print(‘慢走,歡迎下次光臨!!‘) print (‘已購買:%s,餘額:%s‘ % (Shopping_list,TotalSalary)) break Shopping_list.append(AvailableProduct[int(UserChoice)][0]) TotalSalary = int(TotalSalary) - AvailableProduct[int(UserChoice)][1] else : print(‘Insufficient Balance‘) print(‘已購買:%s,餘額:%s‘ % (Shopping_list, TotalSalary)) break
4,測試
python路5__購物車小程式練習