python購物車

購物車程序:python

 1 #! /usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:ShiShen
 4 
 5 product_list = [
 6     ('Iphone' ,5800),
 7     ('Mac' ,12000),
 8     ('Bike' ,900),
 9     ('Coffee' ,31),
10     ('Alex python' ,100),
11     ('watch',4000)
12      ]
13 shoppingcart_list = []
14 salary = input("input your salary:" )
15 if salary.isdigit():
16     salary = int (salary)
17     while True :
18         for index,item in enumerate(product_list) :
19             #print(product_list.index(item),item)
20             print (index,item)
21         user_choise = input("選擇商品:")
22         if user_choise.isdigit():
23             user_choise = int(user_choise)
24             if user_choise <= len(product_list) and  user_choise >= 0:
25                 p_item = product_list[user_choise]
26                 if p_item[1] <= salary:
27                     shoppingcart_list.append(p_item)
28                     salary -= p_item[1]
29                     print("Added %s into shopping cart,your current balance is \033[31;1m %s \033[0m" %(p_item,salary))
30                 else :
31                     print("\033[41;1m 你的餘額只有:%s,請充值\033[0m"%(salary))
32             else:
33                 print ("你選的商品%s不存在"%user_choise)
34 
35         elif user_choise == "q":
36             print("-------shopping list-------")
37             for p in shoppingcart_list:
38                 print(p)
39             print("你餘額:",salary)
40             exit()
41         else :
42             print("請輸入正確的商品編號:")
43 else :
44     print("輸入錯誤,請輸入正確的工資:")
相關文章
相關標籤/搜索