python實現(xiàn)網(wǎng)上購物系統(tǒng)
本文實例為大家分享了python實現(xiàn)網(wǎng)上購物系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1.購物商城的需求分析:
1、輸出歡迎界面還有登錄注冊菜單:
1).登陸
2).注冊 :保留用戶信息,目前做不到固話,先注冊,注冊成功之后再選擇登錄進去
用戶信息包括:用戶名-密碼-初始金額
2.購物菜單:
1).查看商品信息
2).充值金額
3).購買商品
4.查看購物清單
5.退出
2.代碼示例:
簡單實現(xiàn)購物功能,代碼不是非常完善。
不建議將大部分的代碼都寫在函數(shù)體內(nèi),函數(shù)只是用來實現(xiàn)某一個小的功能,能夠進行多次調(diào)用。接受用戶輸入等語句,盡量寫在主菜單里面,而不是函數(shù)體內(nèi)。
"""
@name : shopping.py
@author : xieshan
@projectname: 火影忍者
@time : 2022/3/24
"""
?
#存放已有的原始用戶
user = {'root': {'passwd': '123456', '余額': 300},
? ? ? ? 'admin': {'passwd': '123123', '余額': 400}}
#存放商品信息
dict = {'F001': {'name': '蘋果', 'price': 4.2, 'count': 100}, 'F002': {'name': '香蕉', 'price': 3.2, 'count': 100},
? ? ? ? 'F003': {'name': '棉花糖', 'price': 10, 'count': 100},
? ? ? ? 'F004': {'name': '餅干', 'price': 5.2, 'count': 100}, 'F005': {'name': '芒果', 'price': 9.0, 'count': 100},
? ? ? ? 'F006': {'name': '雞蛋', 'price': 3.0, 'count': 100},
? ? ? ? 'F007': {'name': '果凍', 'price': 3.2, 'count': 100}, 'F008': {'name': '辣條', 'price': 3.5, 'count': 100},
? ? ? ? 'F009': {'name': '牛奶', 'price': 5.0, 'count': 100}}
?
#注冊函數(shù)
def register(uname1, upasswd1, umoney):
? ? if umoney >= 100:
? ? ? ? user.update({uname1: {'passwd': upasswd1, '余額': umoney}})
? ? ? ? print(f"親愛的{uname1},恭喜您注冊成功!您的賬戶余額為{umoney},趕緊去登陸吧!")
? ? elif umoney < 100:
? ? ? ? print(f"您充值的金額低于100,注冊失敗,請重新注冊")
?
#登陸函數(shù)
def login(uname2,upasswd2):
? ? global c
? ? if upasswd2 == user[uname2]['passwd']:
? ? ? ? print(f"歡迎{uname2}用戶登陸成功!您的賬戶余額為{user[uname2]['余額']}")
? ? ? ? c = 1
? ? elif uname2 in user and upasswd2 != user[uname2]['passwd']:
? ? ? ? print(f"抱歉!親愛的{uname2},您的密碼輸入錯誤!請重新輸入!您還有{3 - i}次機會")
? ? ? ? c = 0
?
#購買商品函數(shù)
def shop():
? ? print("購買界面".center(100, '*'))
? ? sig2 = input("請將您選中的商品編號輸入在此(退出請按'q'):")
? ? if sig2 in dict:
? ? ? ? sig3 = input(f"請將{dict[sig2]['name']}的購買數(shù)量輸入在此:")
? ? ? ? if sig3.isdigit():
? ? ? ? ? ? sig3 = int(sig3)
? ? ? ? ? ? if sig3 <= dict[sig2]['count']:
? ? ? ? ? ? ? ? total = sig3 * dict[sig2]['price']
? ? ? ? ? ? ? ? if total <= user[uname2]['余額']:
? ? ? ? ? ? ? ? ? ? umoney = user[uname2]['余額'] - total
? ? ? ? ? ? ? ? ? ? user.update({uname2: {'passwd': upasswd2, '余額': umoney}})
? ? ? ? ? ? ? ? ? ? dict.update({sig2:{'name':dict[sig2]['name'],'price':dict[sig2]['price'],'count':dict[sig2]['count']-sig3}})
? ? ? ? ? ? ? ? ? ? shopcar.append({'商品名稱': dict[sig2]['name'], '購買數(shù)量': sig3})
? ? ? ? ? ? ? ? ? ? print(f"已購買{sig3}個{dict[sig2]['name']},花費{total}元,您的余額為{umoney} ")
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print("抱歉!您的余額不足,不能進行購買!請充值")
? ? ? ? ? ? ? ? ? ? return recharge()
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("抱歉,本商品倉庫數(shù)量不足")
? ? ? ? else:
? ? ? ? ? ? print("您輸入的購買數(shù)量有誤!請重新輸入")
? ? ? ? ? ? return shop()
? ? elif sig2 == 'q':
? ? ? ? pass
? ? else:
? ? ? ? print("您輸入的編號有誤!請重新輸入!")
? ? ? ? return shop()
?
#充值函數(shù)
def recharge():
? ? print("充值界面".center(100, '*'))
? ? print(f"親愛的{uname2}用戶,目前您的賬戶余額為 :{user[uname2]['余額']}元")
? ? r_moeny = input("請輸入您要充值的金額(退出請按q):")
? ? if int(r_moeny) < 50:
? ? ? ? print("充值金額不得低于50哦!")
? ? ? ? return recharge()
? ? if r_moeny == 'q':
? ? ? ? pass
? ? else:
? ? ? ? r_moeny = float(r_moeny)
? ? ? ? umoney = r_moeny + user[uname2]['余額']
? ? ? ? for k in range(1,4):
? ? ? ? ? ? upasswd3 = input("請輸入您的登陸密碼進行驗證: ")
? ? ? ? ? ? if upasswd3 == user[uname2]['passwd']:
? ? ? ? ? ? ? ? user.update({uname2: {'passwd': upasswd2, '余額': umoney}})
? ? ? ? ? ? ? ? print(f"恭喜您,充值成功,目前您的賬戶余額為:{user[uname2]['余額']}元")
? ? ? ? ? ? ? ? break
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print(f"抱歉!親愛的{uname2},您的密碼輸入錯誤!充值失?。≌堉匦螺斎?!您還有{3 - k}次機會")
?
#購物清單函數(shù)
def shop_car():
? ? print("購物清單界面".center(100, '*'))
? ? if shopcar == [] :
? ? ? ? print("小主,這里空空如也,趕緊到三樂購物商城去選購商品吧!")
? ? else:
? ? ? ? print("您的購物清單如下:")
? ? ? ? for j in shopcar:
? ? ? ? ? ? print(j)
?
#主菜單
count = 0
while count == 0:
? ? print("歡迎來到三樂購物系統(tǒng)!".center(100, '-'))
? ? print("1.注冊".center(80))
? ? print("2.登陸".center(80))
? ? print("3.退出".center(80))
? ? option = input("請輸入您的選擇: ")
? ? if option == '1':
? ? ? ? print("登陸界面".center(100, '-'))
? ? ? ? uname1 = input("請設(shè)置您的用戶名(請將用戶名設(shè)置為3-10個字符串的小寫字母): ")
? ? ? ? if uname1 in user.keys():
? ? ? ? ? ? print("用戶名已經(jīng)存在,請重新注冊!")
? ? ? ? elif uname1.islower() and 2 < len(uname1) < 11:
? ? ? ? ? ? upasswd1 = input("請設(shè)置您的密碼:(請將密碼設(shè)置為6位數(shù)字) ")
? ? ? ? ? ? if upasswd1.isdigit() and len(upasswd1) == 6:
? ? ? ? ? ? ? ? umoney = input("請輸入您要充值的金額(初次充值不得低于100):")
? ? ? ? ? ? ? ? umoney = float(umoney)
? ? ? ? ? ? ? ? register(uname1, upasswd1, umoney)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("您設(shè)置的密碼不符合規(guī)范!注冊失敗,請重新注冊")
? ? ? ? else:
? ? ? ? ? ? print("您設(shè)置的用戶名不符合規(guī)范,請重新設(shè)置!")
? ? elif option == '2':
? ? ? ? print("注冊界面".center(100, '-'))
? ? ? ? flag = 0
? ? ? ? while flag == 0:
? ? ? ? ? ? uname2 = input("請輸入您注冊的用戶名: ")
? ? ? ? ? ? if uname2 not in user and uname2 == 'new come':
? ? ? ? ? ? ? ? flag = 1
? ? ? ? ? ? elif uname2 not in user:
? ? ? ? ? ? ? ? print(f"抱歉!{uname2} 此用戶名不存在!請重新輸入或者注冊!輸入'new come'進入菜單頁面")
? ? ? ? ? ? ? ? flag = 0
? ? ? ? ? ? ? ? continue
? ? ? ? ? ? elif uname2 in user:
? ? ? ? ? ? ? ? for i in range(1, 4):
? ? ? ? ? ? ? ? ? ? upasswd2 = input("請輸入您的密碼: ")
? ? ? ? ? ? ? ? ? ? login(uname2,upasswd2)
? ? ? ? ? ? ? ? ? ? if c == 1:
? ? ? ? ? ? ? ? ? ? ? ? flag = 1
? ? ? ? ? ? ? ? ? ? ? ? count = 1
? ? ? ? ? ? ? ? ? ? ? ? shopcar = [] ? ? ? #用列表存放用戶已購買的商品
? ? ? ? ? ? ? ? ? ? ? ? while 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("三樂購物系統(tǒng)".center(100, '#'))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("1、查看商品".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("2、賬戶充值".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("3、購買商品".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("4、查看購物清單".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("5、退出系統(tǒng)".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? option2 = input("請輸入您的選擇:")
? ? ? ? ? ? ? ? ? ? ? ? ? ? if option2 == '1':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("目前三樂購物系統(tǒng)中有的商品信息如下".center(100, "#"))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for i in dict:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print(i, end=': ')
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print(dict[i])
? ? ? ? ? ? ? ? ? ? ? ? ? ? elif option2 == '2':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? recharge()
? ? ? ? ? ? ? ? ? ? ? ? ? ? elif option2 == '3':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shop()
? ? ? ? ? ? ? ? ? ? ? ? ? ? elif option2 == '4':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shop_car()
? ? ? ? ? ? ? ? ? ? ? ? ? ? elif option2 == '5':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("\033[1;36m三樂購物系統(tǒng)歡迎您的下次光臨!\033[0m")
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exit()
? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("您的輸入有誤!請重新輸入!")
? ? ? ? ? ? ? ? print("請重新登陸!或者輸入'new come'進入菜單頁面".center(100, '-'))
? ? elif option == '3':
? ? ? ? print("\033[1;36m三樂購物系統(tǒng)歡迎您的下次光臨!\033[0m")
? ? ? ? exit()
? ? else:
? ? ? ? print("您的輸入有誤,請重新輸入!")以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解python selenium 爬取網(wǎng)易云音樂歌單名
這篇文章主要介紹了python selenium爬取網(wǎng)易云音樂歌單名,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻
pyttsx3是一個開源的Python文本轉(zhuǎn)語音庫,可以將文本轉(zhuǎn)換為自然的人類語音,這篇文章主要介紹了如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻,需要的朋友可以參考下2023-04-04
Python將xml和xsl轉(zhuǎn)換為html的方法
這篇文章主要介紹了Python將xml和xsl轉(zhuǎn)換為html的方法,實例分析了使用libxml2模塊操作xml和xsl轉(zhuǎn)換為html的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
python調(diào)用kubernetesAPI簡單使用方法
這篇文章主要介紹了python調(diào)用kubernetesAPI簡單使用方法,K8s也提供API接口,提供這個接口的是管理節(jié)點的apiserver組件,下文更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-05-05

