python實現淘寶購物系統

 更新時間:2019年10月25日 15:49:31   作者:無冇   我要評論
這篇文章主要為大家詳細介紹了python實現簡易的淘寶購物系統,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

代碼如下:

#剛創建賬戶所擁有的錢
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':'運動鞋','price':200,'count':120},
{'name':'vivo','price':2000,'count':200},
{'name':'自行車','price':2100,'count':300}]

#創建空的購物車
shoppingCar = []

#創建訂單列表
order = []

#注冊賬戶
def register():
 print('→'*8,'注冊賬號','←'*8)
 global account
 global password
 account = input('請輸入賬號')
 password = input('請輸入密碼')
 password1 = input('請確認密碼')
 while True:
 if password == password1:
 print('→'*8,'注冊成功','←'*8)
 log_in()
 break
 else:
 password = input('請重新輸入密碼')
 password1 = input('請確認密碼')


#登錄
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('請選擇您要執行的操作:')
 print('1、查詢商品')
 print('2、查看購物車')
 print('3、查看訂單')
 print('4、其他功能')
 print('5、退出系統')
 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('商品單價:%d'%shoppingCar[i][key])
 elif key == 'count':
 print('商品數量:%d'%shoppingCar[i][key])
 elif key == 'total':
 print('商品總價:%d'%shoppingCar[i][key])
 NeedMoney += shoppingCar[i][key]
 print('一共需花費%d元'%NeedMoney)
 if money >= NeedMoney:
 money -= NeedMoney
 pay_shopping()
 print('一共花費%d元'%NeedMoney)
 else:
 print('余額不足')
 charge_money()

#清空購物車
def pay_shopping():
 print('是否支付 yes / no')
 user = input('')
 if user == 'yes':
 print('支付成功')
 order.extend(shoppingCar)
 shoppingCar.clear( )



#設置充值密碼
def charge_pwd():
 global charge_password
 global money
 print('❀'*30)
 charge_password2 = input('請輸入密碼')
 charge_password1 = input('請確認密碼')
 while True:
 if charge_password1 == charge_password2:
 charge_password = charge_password1
 print('❀'*10,'設置成功','❀'*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
 #總共花費的錢
 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('商品單價:%d'%order[i][key])
 elif key == 'count':
 print('商品數量:%d'%order[i][key])
 elif key == 'total':
 print('商品總價:%d'%order[i][key])
 cost_money += order[i][key]
 print('總共花費%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('商品單價:%d'%dict['price'])
 print('商品庫存:%d'%dict['count'])
 if dict['count'] != 0 : 
 print('請選擇一下功能:\n1、購買\n2、添加至購物車\n3、返回上一項')
 choice = int(input())
 if choice == 1:
  buy_shopping(dict)
  isExist = True
 elif choice == 3:
  search_shopping()
 elif choice == 2:
  num = int(input('請選擇添加至購物車的數量:'))
  while True:
  if num > dict['count']:
  print('超出總量限制,請重新輸入!')
  num = int(input('請選擇添加至購物車的數量:'))
  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('請輸入購買的數量:'))
 if num <= dict['count']:
 needMoney = num * dict['price']
 if money < needMoney:
  print('余額不足,請充值或修改購買數量!')
 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('請選擇您要執行的操作:')
 print('1、充值')
 print('2、更改登錄密碼')
 print('3、更改充值密碼')
 print('4、查看余額')
 choice = int(input())
 if choice == 1:
 print('是否選擇設置充值密碼 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('確認密碼')
 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('確認密碼')
 print('◇'*30)
 if a == b:
 charge_password = a
 break
 else:
 print('重新輸入')

register()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關文章

最新評論

福建体育彩票时时彩11