------------★Python練手項(xiàng)目源碼★------------
Python項(xiàng)目源碼20:銀行管理系統(tǒng)(開(kāi)戶(hù)、查詢(xún)、取款、存款、轉(zhuǎn)賬、鎖定、解鎖、退出)
Python項(xiàng)目19:學(xué)員信息管理系統(tǒng)(簡(jiǎn)易版)
Python項(xiàng)目18:使用Pillow模塊,隨機(jī)生成4位數(shù)的圖片驗(yàn)證碼
Python項(xiàng)目17:教你制作一副帥氣的春聯(lián)
Python項(xiàng)目16:教你使用pillow把女神的圖片,添加表白文字。
Python項(xiàng)目15:Pygame制作,新年動(dòng)態(tài)煙花
Python項(xiàng)目14:使用random,模擬撲克牌發(fā)牌+猜單詞游戲
Python項(xiàng)目12:破解zip壓縮包的密碼
Python項(xiàng)目10:使用Tkinter批量新建文件夾
Python項(xiàng)目09:使用filestools模塊,批量添加圖片水印
Python項(xiàng)目08:用pywin32在聊天窗口發(fā)送QQ好友/群消息
Python小項(xiàng)目07:pywin32實(shí)現(xiàn)自動(dòng)寫(xiě)文字到記事本
Python小項(xiàng)目:05模擬微信發(fā)送好友/群消息
Python小項(xiàng)目05:使用pywifi模塊,暴力破解WIFI密碼 ??!親測(cè)有效
Python經(jīng)典小游戲02:字母數(shù)字代碼雨
這是一個(gè)簡(jiǎn)單的記賬程序,可以記錄收入和支出,以及查詢(xún)收支記錄。程序運(yùn)行時(shí)會(huì)首先檢查數(shù)據(jù)文件account.data是否存在,如果不存在則創(chuàng)建并初始化數(shù)據(jù)。然后進(jìn)入一個(gè)循環(huán),等待用戶(hù)輸入并執(zhí)行相應(yīng)的功能,直到用戶(hù)選擇退出。注意:程序使用了pickle模塊來(lái)序列化和反序列化數(shù)據(jù),以便將數(shù)據(jù)保存在文件中。數(shù)據(jù)文件是一個(gè)二進(jìn)制文件,包含了一個(gè)收支記錄的列表,每個(gè)記錄是一個(gè)包含日期、支出、收入、余額和說(shuō)明的列表。程序的主要功能包括:
cost函數(shù):記錄支出,用戶(hù)輸入支出金額和說(shuō)明,然后將記錄追加到文件中。
save函數(shù):記錄收入,用戶(hù)輸入收入金額和說(shuō)明,然后將記錄追加到文件中。
query函數(shù):查詢(xún)收支記錄,打印出所有記錄。
keep_accounts函數(shù):主程序,根據(jù)用戶(hù)輸入執(zhí)行相應(yīng)的功能,包括開(kāi)銷(xiāo)、收入、查詢(xún)和退出。
# -*- coding:utf-8 -*-
# @Author : 小紅牛
# 微信公眾號(hào):WdPython
import pickle
import time
import os
# 1.記錄開(kāi)銷(xiāo)
def cost(fname):
'用于記錄花費(fèi)'
cost_time = time.strftime('%Y-%m-%d')
try: # 異常處理機(jī)制
cost_deposit = int(input('花銷(xiāo)金額:'))
cost_mark = input('花銷(xiāo)說(shuō)明:')
except ValueError:
print('無(wú)效的金額')
return # 函數(shù)的return類(lèi)似于循環(huán)的break,return提前結(jié)束函數(shù)。
except (KeyboardInterrupt, EOFError):
print('\nbye-bye')
exit(1)
# 在文件中取出所有的收支記錄
with open(fname, 'rb') as f:
records = pickle.load(f)
# 計(jì)算最新余額
balance = records[-1][-2] - cost_deposit
# 構(gòu)建最新一筆收入
record = [cost_time, 0, cost_deposit, balance, cost_mark]
# 將收入追加到收支列表中
records.append(record)
# 將最新收支情況寫(xiě)入文件
with open(fname, 'wb') as fobj:
pickle.dump(records, fobj)
# 2.收入
def save(fname):
save_time = time.strftime('%Y-%m-%d')
try:
save_deposit = int(input('收入金額:'))
save_mark = input('收入說(shuō)明:')
except ValueError:
print('無(wú)效的金額')
return
except (KeyboardInterrupt, EOFError):
print('bye-bye')
exit(1)
with open(fname, 'rb') as fobj:
records = pickle.load(fobj)
balance = records[-1][-2] + save_deposit
record = [save_time, save_deposit, 0, balance, save_mark]
records.append(record)
with open(fname, 'wb') as fobj:
pickle.dump(records, fobj)
# 3.查詢(xún)
def query(fname):
# 用于查賬
# 打印表頭標(biāo)題
print(f'{"date":<15}{"save":<8}{"cost":<8}{"balance":<12}{"mark":<50}')
with open(fname, 'rb') as f:
records = pickle.load(f)
for date, cost, save, balance, mark in records:
print(f'{date:<15}{cost:<8}{save:<8}{balance:<12}{mark:<50}')
# 4.主程序
def keep_accounts():
funcs = {'0': cost, '1': save, '2': query}
prompt = '''*****************
(0)開(kāi)銷(xiāo)
(1)收入
(2)查詢(xún)
(3)退出
*****************
請(qǐng)選擇(0/1/2/3):'''
fname = 'account.data'
if not os.path.exists(fname):
init_data = [[time.strftime('%Y-%m-%d'), 0, 0, 0, '默認(rèn)']]
with open(fname, 'wb') as f:
pickle.dump(init_data, f)
while True:
try:
choice = input(prompt).strip()
except(KeyboardInterrupt, EOFError):
choice = '3'
if choice not in ['0', '1', '2', '3']:
print('輸入的數(shù)字無(wú)效,請(qǐng)重試')
continue
if choice == '3':
print('已經(jīng)退出,歡迎再次使用!')
break
# 執(zhí)行相應(yīng)的功能
funcs[choice](fname)
keep_accounts()
完畢??!感謝您的收看
----------★★歷史博文集合★★----------文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-847335.html
我的零基礎(chǔ)Python教程,Python入門(mén)篇 進(jìn)階篇 視頻教程 Py安裝py項(xiàng)目 Python模塊 Python爬蟲(chóng) Json Xpath 正則表達(dá)式 Selenium Etree CssGui程序開(kāi)發(fā) Tkinter Pyqt5 列表元組字典數(shù)據(jù)可視化 matplotlib 詞云圖 Pyecharts 海龜畫(huà)圖 Pandas Bug處理 電腦小知識(shí)office自動(dòng)化辦公 編程工具 NumPy Pygame文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-847335.html
到了這里,關(guān)于Python項(xiàng)目21:一個(gè)簡(jiǎn)單的記賬系統(tǒng)(收入+支出+查詢(xún))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!