Python 购物车
生活随笔
收集整理的這篇文章主要介紹了
Python 购物车
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
購物車需求:
可購買的商品信息顯示 顯示購物車內的商品信息、數量 購物車內的商品數量進行增加、減少 用戶余額的充值 用戶購買完成進行結賬,將最終余額回寫到用戶文件中?
流程圖:
?
代碼:
?1、主文件?
def login():# 驗證用戶帳號和密碼函數global namelock = "lock.txt"loginfile = "password.txt"login_info = 0i = 0while i < 3 and login_info == 0:name = input("Please input your name: ")with open(lock, "r") as f:for line in f:# if name in line:if name == line.strip():sys.exit('\033[32:1m用戶 %s 已經被鎖定\033[0m' % name)password = input("Please input password: ")with open(loginfile, "r") as f:for line in f:user_file, pass_file = line.split()if user_file == name and pass_file == password:print("Bingo!")login_info = 1return nameelse:print("You name or password is error!")i += 1else:if i == 3 and login_info == 0:f = open(lock, "a")f.write(name + "\n")f.close()print('\033[32:1m用戶 %s 已經被鎖定\033[0m' % name)?
def show_goods():# 顯示當前商店所有商品函數commodity = "commodity.txt"x = PrettyTable(["商品名稱", "價格", "數量"])x.align["商品名稱"] = "l" # 以name字段左對齊x.align["價格"] = "r" # 以name字段右對齊x.align["數量"] = "r" # 以name字段右對齊x.padding_width = 1 # 填充寬度with open(commodity, "r", encoding='utf-8') as f:for line in f:name, money, number = line.split()x.add_row([name, money, number])print(x)?
def show_shopping_cart():# 顯示購物車商品函數open_shop_cart()?
def show_bought_goods():# 顯示已購買商品函數open_bought()?
def show_yue(username):# 顯示帳號的金額money = "money.txt"x = PrettyTable(["用戶", "余額"])x.align["用戶"] = "l" # 以name字段左對齊x.align["余額"] = "r" # 以name字段右對齊x.padding_width = 1 # 填充寬度with open(money, "r", encoding='utf-8') as f:for line in f:name, money = line.split()if name == username:x.add_row([name, money])print(x)?
def show_info():# 顯示信息函數print("*****************************")print("修改密碼,請輸入E:")print("查看購物車,請輸入S:")print("查看已購買商品函數,請輸入V:")print("充值,請輸入M:")print("查詢余額,請輸入Y:")print("購買商品,請輸入B:")print("查看商品數量和價格,請輸入G:")print("添加商品到購物車,請輸入商品名稱:")print("*****************************")return input("請輸入你的選擇:")?
def recharge(username):# 充值函數key = usernamen = input("請輸入充值金額:")filename = "money.txt"edit_file_data(filename, username, n, '+')?
def add_goods_shopping_cart(key):# 添加商品到購物車函數n = input("請輸入購買數量:")file1 = "commodity.txt"file2 = "shop-cart.txt"ret = edit_file_data(file1, key, n, '-')if int(ret) == 1:print("*****************************")print("商品數量不足,請重新選擇")returnelse:edit_file_data(file2, key, n, '+')?
def goods_bought(username):# 從商品購買函數while True:show_shopping_cart()q = input("輸入q返回上級菜單,輸入其他鍵繼續:")if q.lower() == 'q':return# else:key = input("請輸入購買商品名稱:")n = input("請輸入購買數量:")file1 = "money.txt"file2 = "shop-cart.txt"file3 = "bought.txt"with open("commodity.txt", "r", encoding='utf-8') as f1:for line in f1:shop = line.split()if str(shop[0]) == str(key):value = shop[2]sum_value = int(n) * int(value)with open(file1, "r", encoding='utf-8') as f1:for line in f1:shop = line.split()if str(shop[0]) == str(username):value = shop[1]if int(sum_value) > int(value):print("*****************************")print("余額不足請充值:")print("*****************************")return recharge(username)ret = edit_file_data(file2, key, n, '-')if int(ret) == 1:print("*****************************")print("購物車中沒有這么多商品,請重新選擇")print("*****************************")continueelse:edit_file_data(file1, username, sum_value, '-')edit_file_data(file3, key, n, '+')?
def edit_password(username):# 修改帳號密碼函數passwd1 = input("請輸入你的新密碼:")passwd2 = input("請再次確認你的密碼:")if passwd1 != passwd2:print("*****************************")print("你2次輸入的密碼不一致,請重新輸入")print("*****************************")return edit_password(username)else:file1 = "password.txt"file2 = "tmp.txt"with open(file1, "r", encoding='utf-8') as f1:with open(file2, "w", encoding='utf-8') as f2:for line in f1:shop = line.split()if str(shop[0]) == str(username):shop[1] = str(passwd1)dat_out = " ".join(shop)f2.write(dat_out + "\n")else:dat_out = " ".join(shop)f2.write(dat_out + "\n")move_filename(file1)print("*****************************")print("密碼修改成功!")?
def open_shop_cart():x = PrettyTable(["商品名稱", "數量"])x.align["商品名稱"] = "l" # 以name字段左對齊# x.align["價格"] = "r" # 以name字段右對齊x.align["數量"] = "r" # 以name字段右對齊x.padding_width = 1 # 填充寬度with open("shop-cart.txt", "r", encoding='utf-8') as f:for line in f:name, number = line.split()x.add_row([name, number])print("*****************************")print(x)?
def open_commodity():x = PrettyTable(["商品名稱", "價格", "數量"])x.align["商品名稱"] = "l" # 以name字段左對齊x.align["價格"] = "r" # 以name字段右對齊x.align["數量"] = "r" # 以name字段右對齊x.padding_width = 1 # 填充寬度with open("commodity.txt", "r", encoding='utf-8') as f:for line in f:name, money, number = line.split()x.add_row([name, money, number])print(x)?
def open_bought():x = PrettyTable(["商品名稱", "數量"])x.align["商品名稱"] = "l" # 以name字段左對齊x.align["數量"] = "r" # 以name字段右對齊x.padding_width = 1 # 填充寬度with open("bought.txt", "r", encoding='utf-8') as f:for line in f:name, number = line.split()x.add_row([name, number])print(x)?
def move_filename(filename):# 重命名文件名函數for file in os.listdir('.'): # os.listdir('.')遍歷文件夾內的每個文件名,并返回一個包含文件名的listif file == filename:os.remove(file)for file in os.listdir('.'):if file == "tmp.txt":new_name = filenameos.rename(file, new_name)return?
def edit_file_data(filename, key, n, operator):# 修改文件數據函數file2 = "tmp.txt"sign = 0if operator == '+':with open(filename, "r", encoding='utf-8') as f1:with open(file2, "w", encoding='utf-8') as f2:for line in f1:shop = line.split()if str(shop[0]) == str(key):sign = 1shop[1] = str(int(shop[1]) + int(n))dat_out = " ".join(shop)f2.write(dat_out + "\n")else:dat_out = " ".join(shop)f2.write(dat_out + "\n")if sign == 0:shop = [key, str(n)]dat_out = " ".join(shop)f2.write(dat_out + "\n")elif operator == '-':with open(filename, "r", encoding='utf-8') as f1:with open(file2, "w", encoding='utf-8') as f2:for line in f1:shop = line.split()if str(shop[0]) == str(key):if int(shop[1]) < int(n):return 1else:shop[1] = str(int(shop[1]) - int(n))dat_out = " ".join(shop)f2.write(dat_out + "\n")else:dat_out = " ".join(shop)f2.write(dat_out + "\n")move_filename(filename)return 0?
#!/usr/bin/env python # -*- conding:utf-8 -*- # create a shopping cart system# 20170921 # evescnimport sys import os import yaml from prettytable import PrettyTabledef main():# 驗證用戶帳號和密碼username = login()# 顯示當前商店內的所有商品show_goods()while True:# 輸出顯示信息key = show_info()print("*****************************")if key.lower() == 's':show_shopping_cart()elif key.lower() == 'e':edit_password(username)elif key.lower() == 'v':show_bought_goods()elif key.lower() == 'm':recharge(username)elif key.lower() == 'y':show_yue(username)elif key.lower() == 'b':goods_bought(username)elif key.lower() == 'g':show_goods()else:add_goods_shopping_cart(key)if __name__ == "__main__":main()???
?2、用戶帳號密碼文件?
# password.txt evescn 123456 gmkk 12321?
?3、用戶金額文件?
# money.txt evescn 30 gmkk 200?
?4、商品文件??
# commodity.txt 《Python入門》 5 100 《Python放棄》 12 100 瓜子 89 10 蘋果 55 5 西瓜 15 2 葡萄 12 3 西紅柿 8 4
?5、購物車文件??
# shop-cart.txt 瓜子 6 蘋果 2 西瓜 3 西紅柿 2
?6、已購商品文件??
# bought.txt 瓜子 1
運行結果:
??1、修改用戶密碼測試?
Please input your name: evescn Please input password: 123456 Bingo! +----------------+------+------+ | 商品名稱 | 價格 | 數量 | +----------------+------+------+ | 《Python入門》 | 5 | 100 | | 《Python放棄》 | 12 | 100 | | 瓜子 | 89 | 10 | | 蘋果 | 55 | 5 | | 西瓜 | 15 | 2 | | 葡萄 | 12 | 3 | | 西紅柿 | 8 | 4 | +----------------+------+------+ ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:e ***************************** 請輸入你的新密碼:Gmkk 請再次確認你的密碼:gmkk ***************************** 你2次輸入的密碼不一致,請重新輸入 ***************************** 請輸入你的新密碼:gmkk 請再次確認你的密碼:gmkk ***************************** 密碼修改成功! *****************************?
# password.txt 文件內容 evescn gmkk gmkk 12321?
?2、添加商品到購物車測試?
# 原商品文件信息《Python入門》 5 100 《Python放棄》 12 100 瓜子 89 10 蘋果 55 5 西瓜 15 2 葡萄 12 3 西紅柿 8 4# 原購物車文件信息瓜子 6 蘋果 2 西瓜 3 西紅柿 2?
Please input your name: evescn Please input password: 123456 Bingo! +----------------+------+------+ | 商品名稱 | 價格 | 數量 | +----------------+------+------+ | 《Python入門》 | 5 | 100 | | 《Python放棄》 | 12 | 100 | | 瓜子 | 89 | 10 | | 蘋果 | 55 | 5 | | 西瓜 | 15 | 2 | | 葡萄 | 12 | 3 | | 西紅柿 | 8 | 4 | +----------------+------+------+ ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:瓜子 ***************************** 請輸入購買數量:20 ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:?
# 商品文件信息《Python入門》 5 100 《Python放棄》 12 100 瓜子 69 10 蘋果 55 5 西瓜 15 2 葡萄 12 3 西紅柿 8 4# 購物車文件信息瓜子 26 蘋果 2 西瓜 3 西紅柿 2?
?3、充值測試和顯示余額測試?
# 充值前 money.txt 文件 evescn 30 gmkk 200?
***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:m ***************************** 請輸入充值金額:200 ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:y ***************************** +--------+------+ | 用戶 | 余額 | +--------+------+ | evescn | 230 | +--------+------+ *****************************?
?4、顯示購物車?
***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:s ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 26 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ *****************************?
?5、顯示已購商品?
***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:v ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 1 | +----------+------+ *****************************?
?6、購買商品?
***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:b ***************************** ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 26 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ 輸入q返回上級菜單,輸入其他鍵繼續: 請輸入購買商品名稱:瓜子 請輸入購買數量:20 ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 6 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ 輸入q返回上級菜單,輸入其他鍵繼續: 請輸入購買商品名稱:瓜子 請輸入購買數量:5 ***************************** 余額不足請充值: ***************************** 請輸入充值金額:200 ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:b ***************************** ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 6 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ 輸入q返回上級菜單,輸入其他鍵繼續: 請輸入購買商品名稱:瓜子 請輸入購買數量:7 ***************************** 購物車中沒有這么多商品,請重新選擇 ***************************** ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 6 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ 輸入q返回上級菜單,輸入其他鍵繼續: 請輸入購買商品名稱:瓜子 請輸入購買數量:6 ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 0 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ 輸入q返回上級菜單,輸入其他鍵繼續:q ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:?
# 運行后文件數據***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:y ***************************** +--------+------+ | 用戶 | 余額 | +--------+------+ | evescn | 170 | +--------+------+ ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:s ***************************** ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 0 | | 蘋果 | 2 | | 西瓜 | 3 | | 西紅柿 | 2 | +----------+------+ ***************************** 修改密碼,請輸入E: 查看購物車,請輸入S: 查看已購買商品函數,請輸入V: 充值,請輸入M: 查詢余額,請輸入Y: 購買商品,請輸入B: 查看商品數量和價格,請輸入G: 添加商品到購物車,請輸入商品名稱: ***************************** 請輸入你的選擇:v ***************************** +----------+------+ | 商品名稱 | 數量 | +----------+------+ | 瓜子 | 27 | +----------+------+ *****************************?
?已知的bug問題:
沒有對用戶輸入的商品進行判斷,一旦用戶輸入的商品不存在,將會報錯(添加商品到購物車和購買商品時需要輸入商品名稱)
?
轉載于:https://www.cnblogs.com/python-gm/p/7562020.html
總結
以上是生活随笔為你收集整理的Python 购物车的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matcher类的简单使用
- 下一篇: imp导入前对当前用户清库脚本