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

目录结构如下

├── account
│ └── alex.json
│ └── tesla_company.json
├── bin
│ └── start.py
└── core
└── withdraw.py
当执行start.py时,出现交互窗口

———- ICBC Bank ————-

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

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

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

在start.py代码如下

import os
import sys

base_dir = os.path.abspath(os.path.dirname(os.path.dirname(file)))
sys.path.append(base_dir)

from core import withdraw
print(
'ICBC Bank'.center( 30,'-'),' '
'1.账户信息'' '
'2.取现'
)
alex_money = {'user':'alex',}
def choice(num):
if num == '1':
f = open('../account/alex.json','r')
print (type(f.read()))
elif num == '2':
wi_money = input('取现金额').strip()
withdraw.withdraw(wi_money)

choice1 = input('选择服务:')
choice(choice1)

在withdraw代码如下

def withdraw(much):
with open('../account/alex.json','r') as f:
balance = int(float(f.read()))
if balance >= int(much):
with open('../account/alex.json','w') as f:
deduct = balance - int(much)*(1+0.05)
f.write(str(deduct))
elif balance < int(much):
print('余额不足')

原文地址:https://www.cnblogs.com/fengxiongmiao/p/12120950.html