日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python實現網上購物系統_python

作者:萬物皆可愛33 ? 更新時間: 2022-07-30 編程語言

本文實例為大家分享了python實現網上購物系統的具體代碼,供大家參考,具體內容如下

1.購物商城的需求分析:

1、輸出歡迎界面還有登錄注冊菜單:

1).登陸

2).注冊 ?:保留用戶信息,目前做不到固話,先注冊,注冊成功之后再選擇登錄進去

? ? ? ? ? ? ? ? ? 用戶信息包括:用戶名-密碼-初始金額

2.購物菜單:

1).查看商品信息

2).充值金額

3).購買商品

4.查看購物清單

5.退出

2.代碼示例:

簡單實現購物功能,代碼不是非常完善。

不建議將大部分的代碼都寫在函數體內,函數只是用來實現某一個小的功能,能夠進行多次調用。接受用戶輸入等語句,盡量寫在主菜單里面,而不是函數體內。

"""
@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}}
?
#注冊函數
def register(uname1, upasswd1, umoney):
? ? if umoney >= 100:
? ? ? ? user.update({uname1: {'passwd': upasswd1, '余額': umoney}})
? ? ? ? print(f"親愛的{uname1},恭喜您注冊成功!您的賬戶余額為{umoney},趕緊去登陸吧!")
? ? elif umoney < 100:
? ? ? ? print(f"您充值的金額低于100,注冊失敗,請重新注冊")
?
#登陸函數
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
?
#購買商品函數
def shop():
? ? print("購買界面".center(100, '*'))
? ? sig2 = input("請將您選中的商品編號輸入在此(退出請按'q'):")
? ? if sig2 in dict:
? ? ? ? sig3 = input(f"請將{dict[sig2]['name']}的購買數量輸入在此:")
? ? ? ? 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'], '購買數量': sig3})
? ? ? ? ? ? ? ? ? ? print(f"已購買{sig3}個{dict[sig2]['name']},花費{total}元,您的余額為{umoney} ")
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print("抱歉!您的余額不足,不能進行購買!請充值")
? ? ? ? ? ? ? ? ? ? return recharge()
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("抱歉,本商品倉庫數量不足")
? ? ? ? else:
? ? ? ? ? ? print("您輸入的購買數量有誤!請重新輸入")
? ? ? ? ? ? return shop()
? ? elif sig2 == 'q':
? ? ? ? pass
? ? else:
? ? ? ? print("您輸入的編號有誤!請重新輸入!")
? ? ? ? return shop()
?
#充值函數
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}次機會")
?
#購物清單函數
def shop_car():
? ? print("購物清單界面".center(100, '*'))
? ? if shopcar == [] :
? ? ? ? print("小主,這里空空如也,趕緊到三樂購物商城去選購商品吧!")
? ? else:
? ? ? ? print("您的購物清單如下:")
? ? ? ? for j in shopcar:
? ? ? ? ? ? print(j)
?
#主菜單
count = 0
while count == 0:
? ? print("歡迎來到三樂購物系統!".center(100, '-'))
? ? print("1.注冊".center(80))
? ? print("2.登陸".center(80))
? ? print("3.退出".center(80))
? ? option = input("請輸入您的選擇: ")
? ? if option == '1':
? ? ? ? print("登陸界面".center(100, '-'))
? ? ? ? uname1 = input("請設置您的用戶名(請將用戶名設置為3-10個字符串的小寫字母): ")
? ? ? ? if uname1 in user.keys():
? ? ? ? ? ? print("用戶名已經存在,請重新注冊!")
? ? ? ? elif uname1.islower() and 2 < len(uname1) < 11:
? ? ? ? ? ? upasswd1 = input("請設置您的密碼:(請將密碼設置為6位數字) ")
? ? ? ? ? ? if upasswd1.isdigit() and len(upasswd1) == 6:
? ? ? ? ? ? ? ? umoney = input("請輸入您要充值的金額(初次充值不得低于100):")
? ? ? ? ? ? ? ? umoney = float(umoney)
? ? ? ? ? ? ? ? register(uname1, upasswd1, umoney)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("您設置的密碼不符合規范!注冊失敗,請重新注冊")
? ? ? ? else:
? ? ? ? ? ? print("您設置的用戶名不符合規范,請重新設置!")
? ? 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("三樂購物系統".center(100, '#'))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("1、查看商品".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("2、賬戶充值".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("3、購買商品".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("4、查看購物清單".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? print("5、退出系統".center(80))
? ? ? ? ? ? ? ? ? ? ? ? ? ? option2 = input("請輸入您的選擇:")
? ? ? ? ? ? ? ? ? ? ? ? ? ? if option2 == '1':
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("目前三樂購物系統中有的商品信息如下".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三樂購物系統歡迎您的下次光臨!\033[0m")
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exit()
? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("您的輸入有誤!請重新輸入!")
? ? ? ? ? ? ? ? print("請重新登陸!或者輸入'new come'進入菜單頁面".center(100, '-'))
? ? elif option == '3':
? ? ? ? print("\033[1;36m三樂購物系統歡迎您的下次光臨!\033[0m")
? ? ? ? exit()
? ? else:
? ? ? ? print("您的輸入有誤,請重新輸入!")

原文鏈接:https://blog.csdn.net/weixin_43880061/article/details/123750993

欄目分類
最近更新