国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現簡單購物商城

python實現簡單購物商城

2020-08-23 13:52百衲本 Python

這篇文章主要為大家詳細介紹了python實現簡單購物商城的相關資料,具有實用性和一定的參考價值,感興趣的朋友可以參考一下

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

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

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

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

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#coding:utf-8
#Auto:Panwenbin
#function:
import shelve
import time
shop_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=0
def 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 jiage2
def 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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品久久久99| 亚洲精品国产精品国自产在线 | 午夜男人视频 | 亚洲另类视频 | 免费一二区 | av网站在线播放 | 国产精品1区2区3区 久久免费一区 | 狠狠操狠狠干 | 玖玖精品 | 狠狠干网站 | 视频一区 中文字幕 | 亚洲精品二区三区 | 99国产视频 | 精品福利一区二区三区 | 免费成人在线观看 | 亚洲精品免费视频 | 欧美久久视频 | 国产av毛片 | 日韩视频精品在线 | 日本一区二区免费在线播放 | 91视频网页 | 最新国产视频 | 色先锋影院 | 中文字幕一区二区三区在线视频 | 亚洲国产精品一区二区www | 中文字幕免费 | 日韩av一区二区在线观看 | 国产成人在线一区 | 久久久久久久久国产成人免费 | 久久精品成人 | 久久97视频 | 免费欧美一级 | 亚洲免费婷婷 | 精品在线一区 | 视频1区| 99久久久久久 | 免费日韩| 人人干天天干 | 久久久在线免费观看 | 91久久精品国产亚洲a∨麻豆 | 97精品一区二区三区 |