Python小代碼_3_購物車
阿新 • • 發佈:2018-02-10
bubuko one ppi sorry while avi color wan app
product_list = [ (‘MacBook‘, 9000), (‘kindle‘, 500), (‘tesla‘, 900000), (‘book‘, 100), (‘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):print(i, ">>>", v) #引導用戶選擇商品 choice = input("choose goods that you want to buy[exit: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) else: print("-----------------") print("Sorry, your balance is not enough.")print("Your balance: " + str(saving)) print("-----------------") continue print("-----------------") print("You have chose " + p_item[0]) print("Your balance: " + str(saving)) print("-----------------") else: print("Non existent") elif choice == ‘q‘: print("---------------") print("You have chose the following goods:") print("Goods\t\tnumber\tPrice") num = 1 #循環遍歷購物車裏面的商品,購物車存放的是已買商品 for i in shopping_car: print(i[0] + "\t\t" + str(num) + "\t\t" + str(i[1])) print() print("balance :", saving) print("---------------") break else: print("invalid input")
Python小代碼_3_購物車