高亮显示提示
启动程序后,先登录,登录成功则让用户输入充值钱数,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品
# -*- 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": "鼠标", "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('是否进行充值?请输入充值的钱数,回车键继续') if recharge.isdigit(): recharge = int(recharge) dic[input_user]['balance'] += int(recharge) print('当前余额为:', 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('购买成功,花费', 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('是否进行充值?请输入充值的钱数,回车键继续') if recharge.isdigit(): recharge = int(recharge) dic[input_user]['balance'] += int(recharge) print('当前余额为:', 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('您输入的用户名不存在')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。