Python 購物車


Python 購物車

 

  • 需求

 

  1. 用戶名和密碼存放於文件中,格式為:xxx|xxx
  2. 啟動程序后,先登錄,登錄成功則讓用戶輸入工資,然后打印商品列表,失敗則重新登錄,超過三次則退出程序
  3. 允許用戶根據商品編號購買商品
  4. 用戶選擇商品后,檢測余額是否夠,夠就直接扣款,不夠就提醒
  5. 可隨時退出,退出時,打印已購買商品和余額

 

  • 流程圖

 

  • Python代碼實現
  1 #! /usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # 商城購物車
  4 product_list = [
  5     ['Iphone7 Plus',6500],
  6     ['Iphone8 ',8200],
  7     ['MacBook Pro',12000],
  8     ['Python Book',99],
  9     ['Coffee',33],
 10     ['Bike',666],
 11     ['pen',2]
 12 ]
 13 shopping_cart = []
 14 f = open('user.txt','r')
 15 lock_file = f.readlines()
 16 f.close()
 17 count=0
 18 user_list={}
 19 while True:
 20     if count == 3:
 21         print("用戶名輸入次數到達3次限制")
 22         break
 23     for i in lock_file:
 24         i=i.strip()
 25         user_list[i.split('|')[0]]={'password':i.split('|')[1]}
 26     user_name=input("請輸入您的用戶名>>:")
 27     if user_name not in user_list:
 28         print("用戶名錯誤")
 29         count+=1
 30     if user_name in lock_file:
 31         print("用戶名已鎖定,請聯系管理員!")
 32         exit()
 33     if user_name in user_list:
 34         user_password=input("請輸入您的密碼>>: ")
 35         if user_password == user_list[user_name]['password']:
 36             print("歡迎登錄電子商城")
 37             while True:
 38                 salary = input("請輸入您的工資:")  # 輸入金額
 39                 if not salary.isdigit():  # 判斷輸入的salary是不是數字
 40                     print("由於您的輸入的工資不合法,請再次輸入金額")  # 輸入金額不合法
 41                     continue
 42                 else:
 43                     salary = int(salary)  # 把輸入的數字轉成整形
 44                     break
 45             while True:
 46                 print(">> 歡迎來到電子商城 <<")
 47                 for index, i in enumerate(product_list):  # 循環商品列表,商品列表索引
 48                     print("%s.\t%s\t%s" % (index, i[0], i[1]))  # 打印商品列表,顯示商品列表索引
 49                 choice = input(">>請輸入商品序號或輸入 exit 退出商城>>: ").strip()
 50                 if len(choice) == 0:  # 判斷輸入字符串是否為空和字符串長度
 51                     print('-->您沒有選擇商品<--')
 52                     continue
 53                 if choice.isdigit():  # 判斷輸入的choice是不是一個數字
 54                     choice = int(choice)  # 把輸入的字符串轉成整型
 55                     if choice < len(product_list) and choice >= 0:  # 輸入的整數必須小於商品列表的數量
 56                         product_item = product_list[choice]  # 獲取商品
 57                         if salary >= product_item[1]:  # 拿現有金額跟商品對比,是否買得起
 58                             salary -= product_item[1]  # 扣完商品的價格
 59                             shopping_cart.append(product_item)  # 把選着的商品加入購物車
 60                             print("添加 \033[32;1m%s\033[0m 到購物車,您目前的金額是 \
 61             \033[31;1m%s\033[0m" % (product_item[0], salary))
 62                         else:
 63                             print("對不起,您的金額不足,還差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
 64                     else:
 65                         print("-->沒有此商品<--")
 66                 elif choice == "exit":
 67                     total_cost = 0
 68                     print("您的購物車列表:")
 69                     for i in shopping_cart:
 70                         print(i)
 71                         total_cost += i[1]
 72                     print("您的購物車總價是: \033[31;1m%s\033[0m" % (total_cost,))
 73                     print("您目前的余額是:\033[31;1m%s\033[0m" % (salary,))
 74                     break
 75             break
 76         else:
 77             print("密碼錯誤")
 78             count += 1
 79         if count == 3 :
 80             print("您輸入的密碼錯誤次數已達3次,將鎖定您的用戶!")
 81             f = open('blacklist.txt','w')
 82             f.write('%s'%user_name)
 83             f.close()
 84             break
 85 
 86             while True:
 87                 salary = input("請輸入您的工資:")  # 輸入金額
 88                 if not salary.isdigit():  # 判斷輸入的salary是不是數字
 89                     print("由於您的輸入的工資不合法,請再次輸入金額")  # 輸入金額不合法
 90                     continue
 91                 else:
 92                     salary = int(salary)  # 把輸入的數字轉成整形
 93                     break
 94             while True:
 95                 print(">> 歡迎來到電子商城 <<")
 96                 for index, i in enumerate(product_list):  # 循環商品列表,商品列表索引
 97                     print("%s.\t%s\t%s" % (index, i[0], i[1]))  # 打印商品列表,顯示商品列表索引
 98                 choice = input(">>請輸入商品序號或輸入 exit 退出商城>>: ").strip()
 99                 if len(choice) == 0:  # 判斷輸入字符串是否為空和字符串長度
100                     print('-->您沒有選擇商品<--')
101                     continue
102                 if choice.isdigit():  # 判斷輸入的choice是不是一個數字
103                     choice = int(choice)  # 把輸入的字符串轉成整型
104                     if choice < len(product_list) and choice >= 0:  # 輸入的整數必須小於商品列表的數量
105                         product_item = product_list[choice]  # 獲取商品
106                         if salary >= product_item[1]:  # 拿現有金額跟商品對比,是否買得起
107                             salary -= product_item[1]  # 扣完商品的價格
108                             shopping_cart.append(product_item)  # 把選着的商品加入購物車
109                             print("添加 \033[32;1m%s\033[0m 到購物車,\
110                             您目前的金額是 \033[31;1m%s\033[0m"%(product_item[0],salary))
111                         else:
112                             print("對不起,您的金額不足,還差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
113                     else:
114                         print("-->沒有此商品<--")
115                 elif choice == "exit":
116                     total_cost = 0
117                     print("您的購物車列表:")
118                     for i in shopping_cart:
119                         print(i)
120                         total_cost += i[1]
121                     print("您的購物車總價是: \033[31;1m%s\033[0m" % (total_cost,))
122                     print("您目前的余額是: \033[31;1m%s\033[0m" % (salary,))
123                     bre
  1 #! /usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # 商城購物車
  4 product_list = [
  5     ['Iphone7 Plus',6500],
  6     ['Iphone8 ',8200],
  7     ['MacBook Pro',12000],
  8     ['Python Book',99],
  9     ['Coffee',33],
 10     ['Bike',666],
 11     ['pen',2]
 12 ]
 13 shopping_cart = []
 14 f = open('user.txt','r')
 15 lock_file = f.readlines()
 16 f.close()
 17 count=0
 18 user_list={}
 19 while True:
 20     if count == 3:
 21         print("用戶名輸入次數到達3次限制")
 22         break
 23     for i in lock_file:
 24         i=i.strip()
 25         user_list[i.split('|')[0]]={'password':i.split('|')[1]}
 26     user_name=input("請輸入您的用戶名>>:")
 27     if user_name not in user_list:
 28         print("用戶名錯誤")
 29         count+=1
 30     if user_name in lock_file:
 31         print("用戶名已鎖定,請聯系管理員!")
 32         exit()
 33     if user_name in user_list:
 34         user_password=input("請輸入您的密碼>>: ")
 35         if user_password == user_list[user_name]['password']:
 36             print("歡迎登錄電子商城")
 37             while True:
 38                 salary = input("請輸入您的工資:")  # 輸入金額
 39                 if not salary.isdigit():  # 判斷輸入的salary是不是數字
 40                     print("由於您的輸入的工資不合法,請再次輸入金額")  # 輸入金額不合法
 41                     continue
 42                 else:
 43                     salary = int(salary)  # 把輸入的數字轉成整形
 44                     break
 45             while True:
 46                 print(">> 歡迎來到電子商城 <<")
 47                 for index, i in enumerate(product_list):  # 循環商品列表,商品列表索引
 48                     print("%s.\t%s\t%s" % (index, i[0], i[1]))  # 打印商品列表,顯示商品列表索引
 49                 choice = input(">>請輸入商品序號或輸入 exit 退出商城>>: ").strip()
 50                 if len(choice) == 0:  # 判斷輸入字符串是否為空和字符串長度
 51                     print('-->您沒有選擇商品<--')
 52                     continue
 53                 if choice.isdigit():  # 判斷輸入的choice是不是一個數字
 54                     choice = int(choice)  # 把輸入的字符串轉成整型
 55                     if choice < len(product_list) and choice >= 0:  # 輸入的整數必須小於商品列表的數量
 56                         product_item = product_list[choice]  # 獲取商品
 57                         if salary >= product_item[1]:  # 拿現有金額跟商品對比,是否買得起
 58                             salary -= product_item[1]  # 扣完商品的價格
 59                             shopping_cart.append(product_item)  # 把選着的商品加入購物車
 60                             print("添加 \033[32;1m%s\033[0m 到購物車,您目前的金額是 \
 61             \033[31;1m%s\033[0m" % (product_item[0], salary))
 62                         else:
 63                             print("對不起,您的金額不足,還差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
 64                     else:
 65                         print("-->沒有此商品<--")
 66                 elif choice == "exit":
 67                     total_cost = 0
 68                     print("您的購物車列表:")
 69                     for i in shopping_cart:
 70                         print(i)
 71                         total_cost += i[1]
 72                     print("您的購物車總價是: \033[31;1m%s\033[0m" % (total_cost,))
 73                     print("您目前的余額是:\033[31;1m%s\033[0m" % (salary,))
 74                     break
 75             break
 76         else:
 77             print("密碼錯誤")
 78             count += 1
 79         if count == 3 :
 80             print("您輸入的密碼錯誤次數已達3次,將鎖定您的用戶!")
 81             f = open('blacklist.txt','w')
 82             f.write('%s'%user_name)
 83             f.close()
 84             break
 85 
 86             while True:
 87                 salary = input("請輸入您的工資:")  # 輸入金額
 88                 if not salary.isdigit():  # 判斷輸入的salary是不是數字
 89                     print("由於您的輸入的工資不合法,請再次輸入金額")  # 輸入金額不合法
 90                     continue
 91                 else:
 92                     salary = int(salary)  # 把輸入的數字轉成整形
 93                     break
 94             while True:
 95                 print(">> 歡迎來到電子商城 <<")
 96                 for index, i in enumerate(product_list):  # 循環商品列表,商品列表索引
 97                     print("%s.\t%s\t%s" % (index, i[0], i[1]))  # 打印商品列表,顯示商品列表索引
 98                 choice = input(">>請輸入商品序號或輸入 exit 退出商城>>: ").strip()
 99                 if len(choice) == 0:  # 判斷輸入字符串是否為空和字符串長度
100                     print('-->您沒有選擇商品<--')
101                     continue
102                 if choice.isdigit():  # 判斷輸入的choice是不是一個數字
103                     choice = int(choice)  # 把輸入的字符串轉成整型
104                     if choice < len(product_list) and choice >= 0:  # 輸入的整數必須小於商品列表的數量
105                         product_item = product_list[choice]  # 獲取商品
106                         if salary >= product_item[1]:  # 拿現有金額跟商品對比,是否買得起
107                             salary -= product_item[1]  # 扣完商品的價格
108                             shopping_cart.append(product_item)  # 把選着的商品加入購物車
109                             print("添加 \033[32;1m%s\033[0m 到購物車,\
110                             您目前的金額是 \033[31;1m%s\033[0m"%(product_item[0],salary))
111                         else:
112                             print("對不起,您的金額不足,還差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
113                     else:
114                         print("-->沒有此商品<--")
115                 elif choice == "exit":
116                     total_cost = 0
117                     print("您的購物車列表:")
118                     for i in shopping_cart:
119                         print(i)
120                         total_cost += i[1]
121                     print("您的購物車總價是: \033[31;1m%s\033[0m" % (total_cost,))
122                     print("您目前的余額是: \033[31;1m%s\033[0m" % (salary,))
123                     break
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM