首页 > 代码库 > Python小程序之购物车

Python小程序之购物车

 1 goods = [
 2     (IPhone,5888),
 3     (Mac Pro,12888),
 4     (iWatch,2888),
 5     (Bike,888),
 6     (Cofe,16),
 7     (Book,85)
 8 ]
 9 money = int(input("Please Input your Money:"))
10 buylist = []
11 while True:
12     print(商品列表.center(30,-))
13     for index,items in enumerate(goods):
14         name,price = items
15         print(index,name,price)
16     user_choice = input(Please input your choice:)
17     if user_choice.isdigit():
18         user_choice = int(user_choice)
19         if user_choice < len(goods) and user_choice >= 0:
20             if money > goods[user_choice][1]:
21                 money -= goods[user_choice][1]
22                 buylist.append((%s,%s) % (goods[user_choice][0],goods[user_choice][1]) )
23             else:
24                 choice = input(The money are not enough,Continue? )
25                 if choice == y:
26                     continue
27                 else:
28                     print(您的购物车有:)
29                     for info in buylist:
30                         print(info)
31                     exit(您的余额为: %s   % money)
32         else:
33             print(input Error, retry)
34             continue
35     elif user_choice == q:
36         if len(buylist) == 0:
37             exit(您未购买任何东西,您的余额还有:%s % money)
38         else:
39             print(您的购物车有:)
40             for info in buylist:
41                 print(info)
42             print(您的余额为: %s  % money)
43     else:
44         print(\033[31;1mInput Error,retry\033[0m)
45         continue

 

Python小程序之购物车