日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python实现简单的记账本功能

發布時間:2023/12/31 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python实现简单的记账本功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目標:

  1.使用序列化cPickle

  2.賬戶中錢要大于花費的錢,否則提示請存錢

  2.編寫函數,實現存錢,花錢,查詢及退出功能

?

1.序列化

  pickle是python實現序列化的模塊,次模塊存在使用C語言編寫模塊,用法相同,但執行效率更高,所以優先使用C模塊編寫的序列化模塊cPickle。

?

2.編寫函數,實現存錢,花錢,查詢及退出功能

代碼如下:

[root@localhost python]# cat new_account.py

#!/usr/bin/env python # -*- coding: utf-8 -*-import os,time import cPickle as p
def save_money(wallet, record, amount, comment):date = time.strftime("%Y-%m-%d")with open(wallet) as fobj:balance = p.load(fobj) + amountwith open(wallet, 'wb') as fobj:p.dump(balance, fobj)with open(record, 'a') as fobj:fobj.write("%-12s%-8s%-8s%-10s%-20s\n" % (date, 'N/A', amount, balance, comment))def cost_money(wallet, record, amount, comment):date = time.strftime("%Y-%m-%d")with open(wallet) as fobj:balance = p.load(fobj) - amountif balance < 0:print "余額不足,請先存錢或進行其他操作!"else:with open(wallet, 'wb') as fobj:p.dump(balance, fobj)with open(record, 'a') as fobj:fobj.write("%-12s%-8s%-8s%-10s%-20s\n" % (date, amount, 'N/A', balance, comment))def query_money(wallet, record):print "%-12s%-8s%-8s%-10s%-20s" % ('date', 'cost', 'save', 'balance', 'comment')with open(record) as fobj:for line in fobj:print line,with open(wallet) as fobj:print "New Balance:\n%s" % p.load(fobj)def show_menu():w_file = 'wallet.data'r_file = 'record.txt'cmds = {'0': save_money,'1': cost_money,'2': query_money}prompt = """(0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): """if not os.path.isfile(w_file):with open(w_file, 'w') as fobj:p.dump(0, fobj)if not os.path.isfile(r_file):os.mknod(r_file)while True:args = (w_file, r_file)choice = raw_input(prompt).strip()[0]if choice not in '0123':print "Invalid input, Try again."continueif choice in '01':amount = int(raw_input("Amount: "))comment = raw_input("Comment: ")args = (w_file, r_file, amount, comment)if choice == '3':breakcmds[choice](*args)if __name__ == '__main__':print show_menu()

?運行代碼,測試效果

[root@localhost python]# python new_account.py (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment New Balance: 0 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 100 Comment: cost 100 余額不足,請先存錢或進行其他操作! (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 0 Amount: 100 Comment: save 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment 2017-01-06 N/A 100 100 save 100 New Balance: 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 101 Comment: cost 101 余額不足,請先存錢或進行其他操作! (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 100 Comment: cost 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment 2017-01-06 N/A 100 100 save 100 2017-01-06 100 N/A 0 cost 100 New Balance: 0 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3):

?

*附錄

1.如下是自己初次編寫的代碼,函數不具備通用性功能。

#!/usr/bin/env python #coding:utf8import os,sys import time ''' 1.運行該腳本會生成一個balance.txt文件,并設置初始賬戶余額:¥10000 2.運行該腳本會生成一個account.txt文件,并記錄賬戶消費信息詳情。 '''def save():date = time.strftime("%Y-%m-%d")cost = 0while 1:try:save = int(raw_input("請輸入存款金額: ").strip())except ValueError:print "\033[31m請輸入數值類型,重新輸入!\033[0m"continueexcept (KeyboardInterrupt,EOFError):sys.exit("\n\033[31m程序退出\033[0m")if save <= 0:print "\033[31m請輸入一個大于0的存款金額:\033[0m"continuewhile 1:try:comment = str(raw_input("請輸入存款信息: "))except (KeyboardInterrupt,EOFError):sys.exit("\n\033[31m程序退出\033[0m")if not comment:continuebreakbreakbalance = rekcon_balance(save,cost)a.write('%-12s%-12s%-12s%-12s%-12s\n' %(date, cost, save, balance, comment))a.flush()with open('balance.txt', 'w') as b:balance = str(balance)b.write(balance)def cost():save = 0date = time.strftime("%Y-%m-%d")while 1:try:cost = int(raw_input("請輸入消費金額: ").strip())except ValueError:print "\033[31m請輸入數值類型,重新輸入!!!\033[0m"continueexcept (KeyboardInterrupt,EOFError):sys.exit("\n\033[31m程序退出\033[0m")if cost <= 0:print "\033[31m請輸入一個大于0的消費金額:\033[0m"continuebreakbalance = rekcon_balance(save,cost)while balance == -1:print "\033[31m余額不足,請充值或進行其他操作!!!\033[0m"breakelse:while 1:try:comment = str(raw_input("請輸入消費信息: "))except (KeyboardInterrupt,EOFError):sys.exit("\n\033[31m程序退出\033[0m")if not comment:continuebreaka.write('%-12s%-12s%-12s%-12s%-12s\n' %(date, cost, save, balance, comment))with open('balance.txt', 'w') as b:balance = str(balance)b.write(balance)a.flush()def rekcon_balance(save,cost):try:with open('balance.txt', 'r') as b:balance = b.readline()balance = int(balance)except IOError:balance = 10000balance += saveif cost > balance:balance = -1return balancebalance -= cost# with open('balance.txt', 'w') as f:# balance = str(balance)# f.write(balance)return balancedef balance():try:with open('balance.txt', 'r') as b:balance = b.readline()except IOError,e:balance = 10000print "\033[31m初始賬戶余額:\033[0m¥%s" % balanceelse:print "\033[31m當前賬戶余額:\033[0m¥%s" % balancedef view():print '賬戶金額詳細信息'.center(78,'*')print "%-12s%-12s%-12s%-12s%-12s\n" %('Date', 'Cost', 'Save', 'Balance', 'Comment'),with open('account.txt','r') as b:for line in b.readlines():print line,print '*'.center(70,'*') def show_menu():cmds = {'0': save, '1': cost, '2': balance, '3': view, '4': quit}prompt = """\033[32m----------------------------- (0): save money (1): cost money (2): balance (3): view detail (4): quit -----------------------------\033[0m Please Input Your Choice: """while 1:try:choice = raw_input(prompt).strip()[0]except (KeyboardInterrupt,EOFError):sys.exit("\n\033[31m程序退出\033[0m")except IndexError:print "\033[31m無效輸入,請重新輸入!!!\033[0m"continueif choice not in '01234':print "\033[31m無效輸入,請重新輸入!!!\033[0m"continueif choice == 4:breakcmds[choice]()if __name__ == '__main__':a = open('account.txt','a')print show_menu()a.close()

?

轉載于:https://www.cnblogs.com/xkops/p/6257026.html

總結

以上是生活随笔為你收集整理的Python实现简单的记账本功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。