python从零学习day-4 homework:模块导入

最近alex买了个Tesla Model S,通过转账的形式,并且支付了5%的手续费,tesla价格为95万。账户文件为json,请用程序实现该转账行为。

需求如下:

目录结构为

.
├── account
│ ├── alex.json
│ └── tesla_company.json
└── bin
└── start.py

当执行start.py时,出现交互窗口

———- ICBC Bank ————-

  1. 账户信息
  2. 转账

选择1 账户信息 显示alex的当前账户余额。

选择2 转账 直接扣掉95万和利息费用并且tesla_company账户增加95万

对上题增加一个需求:提现。

目录结构如下

.
├── account
│ └── alex.json
│ └── tesla_company.json
├── bin
│ └── start.py
└── core
└── withdraw.py

当执行start.py时,出现交互窗口

———- ICBC Bank ————-

  1. 账户信息
  2. 提现

选择1 账户信息 显示alex的当前账户余额和信用额度。

选择2 提现 提现金额应小于等于信用额度,利息为5%,提现金额为用户自定义。

体现代码的实现要写在withdraw.py里

尝试把上一章的验证用户登陆的装饰器添加到提现和转账的功能上。

import json,os,time,hashlib,sys

# 寻找路径,添加环境变量
root_path = os.path.dirname(os.getcwd())
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #__file__的是打印当前被执行的模块.py文件相对路径,注意是相对路径
# print(BASE_DIR) # 输出是/Users/alex/PycharmProjects/apeland_py_learn/day4_常用模块/my_proj
sys.path.append(BASE_DIR)

# 加载模块
from core import withdraw

# 定义读取
def load(file):
    f = open(r'%s%s'%(root_path,file),'r',encoding='utf-8')
    dict = json.load(f)
    f.close()
    return dict

#定义写入
def dump(file,obj):
    f = open(r'%s%s'%(root_path,file),'w',encoding='utf-8')
    json.dump(obj,f)
    f.close()

# 登录验证(装饰器)
def login(function):
    def inner(*args,**kwargs):
        count = 0
        while count < 3:
            user_name = input('请输入用户名:')
            user_password = input('请输入密码:')
            file_path = root_path + r'\account\%s_info.json' % user_name
            if os.path.exists(file_path) is False:  # 判断用户名是否正确
                print('用户名输错误,请重新输入')
                continue
            else:  # 用户名正确下的一些判定,全部符合运行函数function
                userinfo_dict = load(r'\account\%s_info.json' % user_name)
                if userinfo_dict['status'] == 0:
                    m = hashlib.md5()
                    byte = bytes(user_password,'utf-8')
                    m.update(byte)
                    if userinfo_dict['password'] == m.hexdigest():
                        now = time.time()
                        expire_time = time.mktime(time.strptime(userinfo_dict['expire_date'],'%Y-%m-%d'))
                        if now < expire_time:
                            print('登录成功')
                            userinfo_dict["is_authenticated"] = 1
                            function(*args,**kwargs)
                            dump(r'\account\%s_info.json' %user_name,userinfo_dict)
                        else:
                            print('许可已过期,请联系管理员')
                            break
                    else:
                        print('用户名或密码错误!请重新输入!')
                        count += 1
                        continue
                else:
                    print('账户已锁定,请联系管理员')
                    exit()
        else:
            print('账户已锁定!请联系管理员!')
            userinfo_dict['status'] = 1
            dump(r'\account\%s_info.json' % user_name,userinfo_dict)
            exit()
    return inner  # 此处返回内层函数的内存地址


# 读取信息
user_dict = load(r'\account\alex.json')
company_dict = load(r'\account\Tesla-company.json')
info = '''
———- ICBC Bank ————-
  1.  账户信息
  2.  转账
  3.  提现
'''


# 账户余额
def account_info():
    print('您的账户余额为%s' % user_dict["account_balance"])


# 加了装饰器的转账
@login
def transfer(price,r):
    sum = price * (1 + r)
    if user_dict["account_balance"] >= sum:
        user_dict["account_balance"] = user_dict["account_balance"] - sum
        company_dict["account_balance"] = company_dict["account_balance"] + price
        dump(r'\account\alex.json', user_dict)
        dump(r'\account\Tesla-company.json', company_dict)
        print('购买成功')
        print('您的账户余额为%s' % user_dict["account_balance"])
        exit()
    else:
        print('余额不足')
        print('您的账户余额为%s' % user_dict["account_balance"])
        exit()


# 加了装饰器的提现
@login
def user_withdraw():
    withdraw.withdraw()


# 主程序
while True:
    price = 950000
    r = 0.05
    user_select = input('%s' % info)
    if '1' in user_select:
        account_info()
    elif '2' in user_select:
        transfer(price,r)
        break
    elif'3'in user_select:
        user_withdraw()
        break
    else:
        exit()

其中withdraw文件内容为import os,json,sys

root_path = os.path.dirname(os.getcwd())

def load(file):
    f = open(r'%s%s'%(root_path,file),'r',encoding='utf-8')
    dict = json.load(f)
    f.close()
    return dict


def dump(file,obj):
    f = open(r'%s%s'%(root_path,file),'w',encoding='utf-8')
    json.dump(obj,f)
    f.close()

def withdraw(r=0.05):
    much = input('请输入提现金额:')
    much = int(much)
    user_info = load(r'\account\alex.json')
    # print(user_info)
    sum = much*(1+r)
    if user_info["credit_line"] >= much:
        if user_info["account_balance"] >= sum:
            user_info["account_balance"] = user_info["account_balance"]-sum
            user_info["credit_line"] = user_info["credit_line"] -sum
            dump(r'\account\alex.json', user_info)
            print('提现成功,您的账户余额为%s;\n信用额度为%s。'%(user_info["account_balance"],user_info["credit_line"]))
            exit()
        else:
            print('余额不足!')
            print('您的账户余额为%s。'%(user_info["account_balance"]))
    else:
        print('额度不足!')
        print('您的信用额度为%s。' % (user_info["credit_line"]))
        ```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值