
高亮顯示提示
啟動程序后,先登錄,登錄成功則讓用戶輸入充值錢數(shù),然后打印商品列表,失敗則重新登錄,超過三次則退出程序
允許用戶根據(jù)商品編號購買商品
用戶選擇商品后,檢測余額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,打印已購買商品
# -*- coding:utf-8 -*-
dic = {'alex': {'password': 'qwer', 'count': 0, 'balance': 0},
'lilei': {'password': 'asdf', 'count': 0, 'balance': 0},
'jim': {'password': 'zxcv', 'count': 0, 'balance': 0}}
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標(biāo)", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}, ]
shopping_cart = []
light_s = '\033[1;31;46m%s元\033[0m'
loop = True
while loop:
f = open(file='blacklist.txt', mode='r', encoding='utf-8')
blacklist = f.read()
f.closed
input_user = input('請輸入您的用戶名:').strip()
if input_user in blacklist:
print('很抱歉您的賬戶已被鎖定')
else:
if input_user in dic:
input_password = input('請輸入您的密碼:').strip()
if input_password == dic[input_user]['password']:
print('登錄成功,您的賬戶余額還有', light_s % (dic[input_user]['balance']))
recharge = input('是否進(jìn)行充值?請輸入充值的錢數(shù),回車鍵繼續(xù)')
if recharge.isdigit():
recharge = int(recharge)
dic[input_user]['balance'] += int(recharge)
print('當(dāng)前余額為:', light_s % (dic[input_user]['balance']))
print('商品列表'.center(50, '-'))
print('-序號-', '商品'.center(4, '-'), '價格'.center(4, '-'))
while loop:
for index, i in enumerate(goods):
print(' %s %s %s ' % (index, i['name'], i['price']))
choice = input("請輸入您要購買的商品序號,按'q'鍵退出")
if choice.isdigit():
choice = int(choice)
if choice >= len(goods):
print('\033[1;31;46m您所選擇的商品不存在\033[0m')
continue
if dic[input_user]['balance'] >= int(goods[choice]['price']):
dic[input_user]['balance'] -= int(goods[choice]['price'])
print('購買成功,花費(fèi)', light_s % (goods[choice]['price']), '余額還剩',
light_s % dic[input_user]['balance'])
shopping_cart.append(goods[choice])
else:
print('余額不足,您的賬戶余額還有', light_s % (dic[input_user]['balance']))
recharge = input('是否進(jìn)行充值?請輸入充值的錢數(shù),回車鍵繼續(xù)')
if recharge.isdigit():
recharge = int(recharge)
dic[input_user]['balance'] += int(recharge)
print('當(dāng)前余額為:', light_s % (dic[input_user]['balance']))
elif choice == 'q':
print('您購買的商品列表清單如下')
for i in shopping_cart:
print(i['name'], i['price'])
print('\033[1;31;46m歡迎再來\033[0m')
loop = False
else:
print('\033[1;31;46m請輸入正確的序號\033[0m')
else:
print('\033[1;31;46m密碼錯誤\033[0m')
dic[input_user]['count'] += 1
if dic[input_user]['count'] > 2:
f = open(file='blacklist.txt', mode='a', encoding='utf-8')
f.write('%s ' % input_user)
print('\033[1;31;46ml您的賬號已被加入黑名單\033[0m')
f.closed
break
else:
print('您輸入的用戶名不存在')另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標(biāo)題:代碼練習(xí),購物車-創(chuàng)新互聯(lián)
文章URL:http://www.jbt999.com/article46/ccppeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、軟件開發(fā)、網(wǎng)站排名、虛擬主機(jī)、網(wǎng)站維護(hù)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容