您好,登錄后才能下訂單哦!
高亮顯示提示
啟動(dòng)程序后,先登錄,登錄成功則讓用戶輸入充值錢數(shù),然后打印商品列表,失敗則重新登錄,超過三次則退出程序
允許用戶根據(jù)商品編號(hào)購(gòu)買商品
用戶選擇商品后,檢測(cè)余額是否夠,夠就直接扣款,不夠就提醒
可隨時(shí)退出,退出時(shí),打印已購(gòu)買商品
# -*- 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('請(qǐng)輸入您的用戶名:').strip() if input_user in blacklist: print('很抱歉您的賬戶已被鎖定') else: if input_user in dic: input_password = input('請(qǐng)輸入您的密碼:').strip() if input_password == dic[input_user]['password']: print('登錄成功,您的賬戶余額還有', light_s % (dic[input_user]['balance'])) recharge = input('是否進(jìn)行充值?請(qǐng)輸入充值的錢數(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('-序號(hào)-', '商品'.center(4, '-'), '價(jià)格'.center(4, '-')) while loop: for index, i in enumerate(goods): print(' %s %s %s ' % (index, i['name'], i['price'])) choice = input("請(qǐng)輸入您要購(gòu)買的商品序號(hào),按'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('購(gòu)買成功,花費(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)行充值?請(qǐng)輸入充值的錢數(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('您購(gòu)買的商品列表清單如下') 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請(qǐng)輸入正確的序號(hào)\033[0m') else: print('\033[1;31;46m密碼錯(cuò)誤\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您的賬號(hào)已被加入黑名單\033[0m') f.closed break else: print('您輸入的用戶名不存在')
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。