简单的家庭理财程序

#/user/bin/env python
#-*-coding:utf-8-*-
"家庭理财程序"

import os

family_account = 'account.cfg'

def Home():
    """主页菜单"""
    menu = """
        储蓄(Savings ...s)
        支票(Check ...k)
        金融市场(The financial markets ...m)
        退出(Exit ...e)
        请选择: 
    """
    items = {'s' : '储蓄', 'k' : '支票', 'm' : '金融市场', 'e' : '退出'}
    while True:
        try:
            choice = raw_input(menu).strip()[0].lower()
        except EOFError, KeyboardInterrupt:
            choice = 'e'

        if choice not in 'skme':
            print 'invalid option, try again'
            continue
        else:
            print '进入[%s]页面' % items[choice]

        if choice == 's':
            Saving()
        elif choice == 'k':
            pass
        elif choice == 'm':
            pass
        elif choice == 'e':
            break


def Saving():
    '''储存页面'''
    menu = """
        存款(Deposit ...d)
        取款(Withdrawal ...w)
        借款(Borrow money ...b)
        贷款(Loans ...l)
        取消(Cancel ...c)
        请选择:
    """
    items = {'d' : '存款', 'w' : '取款', 'b' : '借款', 'l' : '贷款', 'c' : '取消'}
    savings = {'deposit' : 0.0, "borrow" : 0.0, 'loans' : 0.0}
    while True:
        try:
            choice = raw_input(menu).strip()[0].lower()
        except EOFError, KeyboardInterrupt:
            choice = 'e'

        if choice not in 'dwblc':
            print 'invalid option, try again'
            continue
        else:
            print '进入[%s]页面' % items[choice]

        if choice == 'd':
            Deposit()
        elif choice == 'w':
            Withdrawal()
        elif choice == 'b':
            pass
        elif choice == 'l':
            pass
        elif choice == 'c':
            break

def ReadAccount():
    account_info = {}
    if not os.path.exists(family_account):
        return account_info
    f = open(family_account, "r")
    for eachline in f:
        pos = eachline.find('=')
        if pos == -1:
            continue
        key = eachline[0:pos]
        value = eachline[pos+1 : -1]
        account_info[key] = float(value)

    f.close()
    return account_info

def SaveAccount(account_info):
    f = open(family_account, "w")
    item = ""
    for key, value in account_info.items():
        item = key
        item += "="
        item += str(value)
        f.write(item)
    f.close()
    return True

def Deposit():
    deposit = 0.0
    account = ReadAccount()
    if "deposit" in account.keys():
        deposit = account["deposit"]
    print "你的存款余额有:¥%.2f" % deposit
    money = raw_input("放入存款金额:")
    money = float(money)
    if money < 0.0:
        return

    print "开始存款 ...请稍后"
    deposit += money
    account["deposit"] = deposit
    SaveAccount(account)
    print "存款成功..."

def Withdrawal():
    balance = 0.0
    account = ReadAccount()
    if "deposit" in account.keys():
        balance = account["deposit"]
    print "你的存款余额有:¥%.2f" % balance
    money = raw_input("输入取款金额:")
    money = float(money)
    if money < 0.0:
        print "取款结束"
        return
    print "开始取款 ...请稍后"
    balance -= money
    if balance <= 0.0:
        print "余额不足"
        return
    account["deposit"] = balance
    SaveAccount(account)
    print "本次取款为 : ¥%.2f" % money

if __name__ == '__main__':
    Home()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值