python實(shí)現(xiàn)淘寶購物系統(tǒng)
更新時(shí)間:2019年10月25日 15:49:31 作者:無冇
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡易的淘寶購物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python淘寶購物系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
代碼如下:
#剛創(chuàng)建賬戶所擁有的錢
money = 0
#定義商品列表
goods_list = [
{'name':'iphone','price':4500,'count':40},
{'name':'電腦','price':7000,'count':100},
{'name':'平板','price':5000,'count':60},
{'name':'羽絨服','price':500,'count':80},
{'name':'西服','price':1000,'count':90},
{'name':'運(yùn)動鞋','price':200,'count':120},
{'name':'vivo','price':2000,'count':200},
{'name':'自行車','price':2100,'count':300}]
#創(chuàng)建空的購物車
shoppingCar = []
#創(chuàng)建訂單列表
order = []
#注冊賬戶
def register():
print('→'*8,'注冊賬號','←'*8)
global account
global password
account = input('請輸入賬號')
password = input('請輸入密碼')
password1 = input('請確認(rèn)密碼')
while True:
if password == password1:
print('→'*8,'注冊成功','←'*8)
log_in()
break
else:
password = input('請重新輸入密碼')
password1 = input('請確認(rèn)密碼')
#登錄
def log_in():
print('~'*10,'登錄賬號','~'*10)
while True:
user_account = input('請輸入您的賬號')
user_password = input('請輸入您的密碼')
if user_account != account:
print('賬號有誤')
elif user_account == account and user_password != password:
print('密碼有誤')
else:
print('~'*10,'登錄成功','~'*10)
show_menu()
break
#展示商品列表
def show_name():
print('❤'*30)
a = 0
for i in range(0,len(goods_list)):
for key in goods_list[i].keys():
if key == 'name':
a += 1
print(goods_list[i][key], end = '\t')
if a % 4 ==0 :
print('')
print('❤'*30)
#選擇操作
def show_menu():
while True:
print('※'*20)
print('請選擇您要執(zhí)行的操作:')
print('1、查詢商品')
print('2、查看購物車')
print('3、查看訂單')
print('4、其他功能')
print('5、退出系統(tǒng)')
print('※'*20)
choice = int(input())
if choice == 1:
show_name()
search_shopping()
elif choice == 2:
show_shoppingCar()
elif choice == 3:
show_order()
elif choice == 4:
other()
else:
print('歡迎下次光臨!')
break
#添加商品至購物車
def add_shopping(name,price,count,total):
dict = {}
dict['name'] = name
dict['price'] = price
dict['count'] = count
dict['total'] = total
shoppingCar.append(dict)
#展示購物車
def show_shoppingCar():
global money
NeedMoney = 0
for i in range(0,len(shoppingCar)):
for key in shoppingCar[i].keys():
print('*'*30)
if key == 'name':
print('商品名稱:'+shoppingCar[i][key])
elif key == 'price':
print('商品單價(jià):%d'%shoppingCar[i][key])
elif key == 'count':
print('商品數(shù)量:%d'%shoppingCar[i][key])
elif key == 'total':
print('商品總價(jià):%d'%shoppingCar[i][key])
NeedMoney += shoppingCar[i][key]
print('一共需花費(fèi)%d元'%NeedMoney)
if money >= NeedMoney:
money -= NeedMoney
pay_shopping()
print('一共花費(fèi)%d元'%NeedMoney)
else:
print('余額不足')
charge_money()
#清空購物車
def pay_shopping():
print('是否支付 yes / no')
user = input('')
if user == 'yes':
print('支付成功')
order.extend(shoppingCar)
shoppingCar.clear( )
#設(shè)置充值密碼
def charge_pwd():
global charge_password
global money
print('❀'*30)
charge_password2 = input('請輸入密碼')
charge_password1 = input('請確認(rèn)密碼')
while True:
if charge_password1 == charge_password2:
charge_password = charge_password1
print('❀'*10,'設(shè)置成功','❀'*10)
show_menu()
break
#充值金額
def charge_money():
global money
print('是否充值 yes / no')
user = input('')
if user == 'yes':
while True:
user = input('請輸入密碼')
if user == charge_password:
while True:
chargeMoney = int(input('請輸入充值金額'))
if chargeMoney % 100 != 0:
print('請輸入充值金額')
else:
money += chargeMoney
print('充值成功')
break
break
else:
print('密碼有誤')
#添加至訂單
def add_order(name,price,count,total):
dict = {}
dict['name'] = name
dict['price'] = price
dict['count'] = count
dict['total'] = total
order.append(dict)
#展示訂單
def show_order():
cost_money = 0
#總共花費(fèi)的錢
for i in range(0,len(order)):
for key in order[i].keys():
print('*'*50)
if key == 'name':
print('商品名稱:'+order[i][key])
elif key == 'price':
print('商品單價(jià):%d'%order[i][key])
elif key == 'count':
print('商品數(shù)量:%d'%order[i][key])
elif key == 'total':
print('商品總價(jià):%d'%order[i][key])
cost_money += order[i][key]
print('總共花費(fèi)%d元'%cost_money)
#查找商品
def search_shopping():
name = input('請輸入您要查詢的名稱:')
isExist = False
for i in range(0,len(goods_list)):
if isExist:
isExist = False
break
dict = goods_list[i]
if dict['name'] == name:
print('商品名稱:'+name)
print('商品單價(jià):%d'%dict['price'])
print('商品庫存:%d'%dict['count'])
if dict['count'] != 0 :
print('請選擇一下功能:\n1、購買\n2、添加至購物車\n3、返回上一項(xiàng)')
choice = int(input())
if choice == 1:
buy_shopping(dict)
isExist = True
elif choice == 3:
search_shopping()
elif choice == 2:
num = int(input('請選擇添加至購物車的數(shù)量:'))
while True:
if num > dict['count']:
print('超出總量限制,請重新輸入!')
num = int(input('請選擇添加至購物車的數(shù)量:'))
else:
add_shopping(dict['name'],dict['price'],num,dict['price']*num)
isExist = True
print('添加成功')
break
else:
print('輸入有誤,再見!')
else:
if i == len(goods_list)-1:
print('該商品不存在,請重新選擇功能!')
#購買商品
def buy_shopping(dict):
global money
if dict['count'] == 0:
print('該商品已售空,請選擇其他商品')
else:
while True:
num = int(input('請輸入購買的數(shù)量:'))
if num <= dict['count']:
needMoney = num * dict['price']
if money < needMoney:
print('余額不足,請充值或修改購買數(shù)量!')
else:
money -= needMoney
dict['count'] -= num
print('購買成功!')
add_order(dict['name'],dict['price'],num,dict['price']*num)
break
else:
print('庫存不足,請重新輸入')
#其他功能
def other():
print('△'*30)
print('請選擇您要執(zhí)行的操作:')
print('1、充值')
print('2、更改登錄密碼')
print('3、更改充值密碼')
print('4、查看余額')
choice = int(input())
if choice == 1:
print('是否選擇設(shè)置充值密碼 yes/ no')
a = input()
if a == 'yes' :
charge_pwd()
else:
charge_money()
elif choice == 2:
change_password()
elif choice == 3:
changeCPWD()
elif choice == 4:
print('余額為%d元'%money)
#更改登錄密碼
def change_password():
global password
while True:
print('☆'*30)
a = input('輸入新密碼')
b = input('確認(rèn)密碼')
print('☆'*30)
if a == b:
password = a
print('請重新登錄')
log_in()
break
else:
print('重新輸入')
#更改支付密碼
def changeCPWD():
global charge_password
while True:
print('◇'*30)
a = input('輸入新密碼')
b = input('確認(rèn)密碼')
print('◇'*30)
if a == b:
charge_password = a
break
else:
print('重新輸入')
register()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實(shí)現(xiàn)數(shù)組相減示例
今天小編就為大家分享一篇Python 實(shí)現(xiàn)數(shù)組相減示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
15行Python代碼實(shí)現(xiàn)網(wǎng)易云熱門歌單實(shí)例教程
這篇文章主要給大家介紹了關(guān)于利用15行Python代碼實(shí)現(xiàn)網(wǎng)易云熱門歌單的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
OpenCV學(xué)習(xí)之圖像形態(tài)學(xué)處理詳解
這篇文章主要為大家詳細(xì)介紹了OpenCV中圖像形態(tài)學(xué)處理的相關(guān)知識,例如:腐蝕操作、膨脹操作、開閉運(yùn)算、梯度運(yùn)算、Top Hat Black Hat運(yùn)算等操作,需要的可以參考一下2023-02-02
TensorFlow神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)之張量與變量概念
這篇文章主要為大家介紹了TensorFlow神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)的基本知識張量與變量概念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

