国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現簡單購物商城

2020-01-04 17:27:16
字體:
來源:轉載
供稿:網友
這篇文章主要為大家詳細介紹了python實現簡單購物商城的相關資料,具有實用性和一定的參考價值,感興趣的朋友可以參考一下
 

 本文為大家分享了購物商城小程序,供大家參考,具體內容如下

軟件版本:python3.x
功能:實現簡單購物商城
1.允許用戶選擇購買多少件
2.允許多用戶登錄,下一次登錄后,繼續按上次的余額繼續購買
3. 允許用戶查看之前的購買記錄(顯示購買時間) 
4. 商品列表分級展示

操作:
1.默認用戶,pan,li,密碼為123
2.登錄后需正確輸入用戶名和密碼
3.按提示選擇充值的金額
4.選擇購買的商品,按q退出,按c查看易購買記錄,按s查看當前已購買商品

注:file_lock.txt,username_file.txt需事先創建

代碼如下:

#!/usr/bin/env python#coding:utf-8#Auto:Panwenbin#function:import shelveimport timeshop_list={ #定義商品清單 '數碼電器':{'電腦':'8000','照相機':'10000','手機':'3800','打印機':'3600'}, '服裝百貨':{'牛仔褲':'288','夾克':'300','王老吉':'6','方便面':'4'}, '汽車':{'特斯拉':'999999','寶馬X5':'550000','帕沙特':'250000','奇瑞':'100000'}, '化妝品':{'歐萊雅':'888','歐詩漫':'666','韓束':'388','百雀羚':'259'},}count=0#定義一個計數器jiage=[]shop_list1={}shop_name=list(shop_list.keys()) #將商品第一層清單數據類型轉換為列表lock_file=open('file_lock.txt','r+')user_pass=open('username_file.txt','r+')count=0def show_list():#打印序列號與商品分類清單 for i,j in enumerate(shop_list):  print('%d:%s'%(i,j))def show_menu(user_choise):#打印序列號與商品第二層清單 user_choise=int(user_choise) a=shop_name[user_choise] for index,key in enumerate(shop_list[a]): #打印序列號與商品第二層清單  print('%d:%s %s'%(index,key,shop_list[a][key]))def show_menu2(user_choise,user_choise2):#調用用戶第一次選擇和第二次選擇,進入對應的購物列表  user_choise=int(user_choise)  if user_choise2.isdigit() and int(user_choise2) < len(shop_list[shop_name[int(user_choise)]]):#判斷用戶輸入的是否為數字,并小于商品序列號   user_choise2=int(user_choise2)  # if user_choise2==0:  jiage.clear()  for v,k in enumerate(shop_list[shop_name[user_choise]]): #獲取用戶進入第二層商品的清單   jiage.append(shop_list[shop_name[user_choise]][k])#將獲取的商品清單存入列表中  jiage2=(int(jiage[user_choise2]))  global jiage2def quit_time():#查詢完成后退出 for i in [3,2,1]:  print('/033[32;1m查詢完畢,正在返回主菜單...../033[1m',i)  time.sleep(1)def start(): while True:  if user_choise.isdigit() and int(user_choise) <len(shop_list):#判斷用戶輸入的是否為數字,并小于商品序列號    show_menu(user_choise)#調用show_menu函數,    break  else:   print('/033[31;1m無效選項,請重新輸入/033[1m')#用戶重新輸入   break########################################################################################################################while count <3:#只要重試不超過3次就不斷循環 print('/033[31;1m默認用戶名密碼為:pan,123 li,123/033[1m') username=input('/033[34;1m請輸入您的用戶名:/033[1m') for i in lock_file.readlines(): #判斷用戶名是否在鎖文件中  i=i.split()  if username in i[0]:   print('/033[31;1m對不起 %s 已鎖定/033[1m'%username)   exit() match = False for j in user_pass.readlines():  user,password = j.strip('/n').split() #去掉每行多余的/n并把這一行按空格分成兩列,分別賦值為user,passwd兩個變量  if username == user: #判斷輸入的用戶是否存在    passwd=input ('/033[30;1m請輸入密碼:')    if password == passwd:      match = True      break    elif password != passwd:#在用戶名正確的前提下,判斷輸入的密碼是否正確      for i in range(2):      passwd=input ('/033[31;1m密碼錯誤,請重新輸入密碼:/033[1m')      if password == passwd:        match =True        break      else:      print('/033[31;1m密碼和用戶名不匹配,嘗試超過三次,用戶被鎖定/033[1m')      lock_file.write('%s /n'%username)      lock_file.close()      user_pass.close()      exit() if username != user:   print('/033[31;1m您輸入用戶名不存在,程序已退出/033[1m')   exit() elif match==True:  break########################################################################################################################f=shelve.open('user.db','c+')try: if f[user] >0:  pay=f[user]  chong_zhi=input('/033[33;1m您的當前余額為%d,是否充值?充值請輸入您要充值的金額,任意鍵進入下一步,退出程序請按q:/033[1m'%f[user])  if chong_zhi.isdigit() and int(chong_zhi) > 0:   pay=int(chong_zhi)+int(pay)   print('/033[35;1m充值后金額為/033[1m',pay)except KeyError: chong_zhi=input('/033[36;1m您的當前余額為0,是否充值?充值請輸入您要充值的金額,退出程序請按q:/033[1m') while True:  if chong_zhi.isdigit() and int(chong_zhi) > 0:   pay=int(chong_zhi)   print('/033[33;1m充值后金額為/033[1m',pay)   break  elif chong_zhi=='q':   print('/033[36;1m程序正在退出/033[1m')   exit()  else:   pass#  pay=input('您的當前余額為0,是否充值?充值請輸入您要充值的金額,退出程序請按q:')########################################################################################################################while True: show_list()#調用show_list函數,打印商品分類清單 user_choise=input('/033[32;1m選擇購買商品的類型:/033[1m') #獲取用戶選擇商品的分類 start()#調用start函數 user_choise2=input('/033[36;1m選擇購買商品的類型。按q退出,按c查看易購買記錄,按s查看當前已購買商品,任意鍵返回上一級菜單,:/033[0m')#獲取用戶選擇的商品# user_num=input('/033[35;1m請選擇需要購買的件數,默認為1:/033[0m')#獲取用戶選擇商品的數量 if user_choise2.isdigit() and int(user_choise2) < len(shop_list[shop_name[int(user_choise)]]):#判斷用戶輸入的是否為數字,并小于商品序列號   user_num=input('/033[35;1m請選擇需要購買的件數,默認為1:/033[1m')#獲取用戶選擇商品的數量   show_menu2(user_choise,user_choise2)#調用show_menu2函數,獲取用戶選擇商品的種類和數量,可用金額是否滿足   if user_num.strip() =='':#如果用戶輸入為空,默認為1    user_num=int(1)   elif user_num.isdigit and int(user_num) > 1:#如果用戶輸入是數字切大于1,獲得用戶輸入中   user_num=int(user_num)   else:   user_num=int(1)#其余情況下默認為一   pay=int(pay)   if pay > jiage2*user_num:#判斷用戶選擇商品的價格*數量是否可以支付   pay=pay-jiage2*user_num   choise=list(shop_list[shop_name[int(user_choise)]].keys())#得到用戶進入第二層商品列表   count+=1   #計數器值加一   goumai_jilu=shelve.open('goumai_jilu.txt','a+') #調用shelve打開一個數據文件   goumai_jilu[str(count)]=choise[int(user_choise2)],jiage2,user_num,time.ctime()#向數據文件中插入用戶選擇的商品,個數和購買時間   goumai_jilu.close()#關閉數據文件   a=choise[int(user_choise2)]#得到用戶選擇的商品,choise為用戶選擇第二層商品列表   if a in shop_list1:  #如果用戶選擇的商品已購買    shop_list1[a][0]=int(shop_list1[a][0])+user_num #將購買數加一    shop_list1[a][1]=int(shop_list1[a][1])+int(shop_list1[a][1]*user_num) #將總額加   else:    shop_list1[a]=list([1,jiage2])   print('/033[32;1m您的余額為:/033[1m',pay)   else:   print('/033[32;1m您的余額不足,請重新選擇:/033[0m') elif user_choise2=='b':#返回商品分類清單  continue elif user_choise2=='q':#退出程序  f=shelve.open('user.db','a+')  f[user]=pay  f.close()  exit() elif user_choise2=='c':  goumai_jilu=shelve.open('goumai_jilu.txt','a+')  print('/033[32;1m您的當前購買記錄為:/033[1m')  for i in goumai_jilu.items(): #獲取購買記錄中的數據   print('{} {}'.format(i[0],goumai_jilu[i[0]]))#打印購買記錄  print('/033[32;1m################################################/033[0m')  quit_time() elif user_choise2=='s':  print('/033[32;1mp_name  num   total_price/033[1m')  print('/033[32;1m%-10s%-10s%-10s/033[1m'%(a,shop_list1[a][0],shop_list1[a][1]))#打印當前購買的商品,個數以及總額  print('/033[32;1m################################################/033[0m')  quit_time() else:  print('/033[41;33m無效選項,請重新選擇:/033[0m')  continue

以上就是本文的全部內容,希望對大家學習Python程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌阳县| 沙雅县| 兴海县| 浮梁县| 施甸县| 曲水县| 黄骅市| 会宁县| 平昌县| 天峨县| 浮山县| 商城县| 个旧市| 临桂县| 东港市| 应城市| 都匀市| 洮南市| 乐清市| 广昌县| 广安市| 叙永县| 平罗县| 鄯善县| 华池县| 修水县| 合江县| 敦化市| 普兰县| 连江县| 若羌县| 固原市| 牟定县| 邹城市| 精河县| 介休市| 高青县| 安达市| 红安县| 三台县| 喜德县|