9–10. 家庭理財. 建立乙個家庭理財程式. 你的程式需要處理儲蓄, 支票, 金融市場, 定期存款等多種帳戶. 為每種帳戶提供乙個選單操作介面, 要有存款, 取款, 借, 貸等操作. 另外還要提供乙個取消操作選項. 使用者退出這個程式時相關資料應該儲存到檔案裡去(出於備份的目的,程式執行過程中也要備份.)
有兩天沒學寫程式了,好墮落。
def deposit(money,string):
'''存/取款
''' prompt = """
(y)確認
(n)返回
enter choice: """
if string==1:
plus=int(input('請輸入您存入的金額:'))
if string==2:
plus=-int(input('請輸入您取出的金額:'))
done = false
while not done:
chosen = false
while not chosen:
try:
choice = input(prompt).strip()[0].lower()
except (eoferror, keyboardinterrupt):
choice = 'n'
print ('\nyou picked: [%s]' % choice)
if choice not in 'yn':
print ('invalid option, try again')
else:
if choice == 'y':
money=money+plus
return money
break
done = true
def loan(loans,choise):
'''取款'''
prompt = """
(y)確認
(n)返回
enter choice: """
if choise==1:
plus=int(input('請輸入你要借出的金額:'))
if choise==2:
plus=-int(input('請輸入您貸入的金額:'))
done = false
while not done:
chosen = false
while not chosen:
try:
choice = input(prompt).strip()[0].lower()
except (eoferror, keyboardinterrupt):
choice = 'n'
print ('\nyou picked: [%s]' % choice)
if choice not in 'yn':
print ('invalid option, try again')
else:
if choice == 'y':
loans=loans+plus
return loans
break
done = true
def showmenu():
'''主選單'''
prompt = """
(1)存款
(2)取款
(3)借款
(4)貸款
(5)顯示
(q)uit
enter choice: """
done = false
f=open('c:/python34/learn/data','r')
if 'money' not in f.read():
g=open('c:/python34/learn/data','w')
g.write('money:0\nloan:0')
g.close()
f.seek(0)
a=f.readline().strip()
b=f.readline().strip()
money=int(a[6:])
loans=int(b[5:])
f.close()
while not done:
chosen = false
while not chosen:
try:
choice = input(prompt).strip()[0].lower()
except (eoferror, keyboardinterrupt):
choice = 'q'
print ('\nyou picked: [%s]' % choice)
if choice not in '12345q':
print ('invalid option, try again')
else:
if choice == '1':
money=deposit(money,1)
elif choice == '2':
money=deposit(money,2)
elif choice == '3':
loans=loan(loans,1)
elif choice == '4':
loans=loan(loans,2)
elif choice == '5':
print('money:%d'%money)
print('loan:%d'%loans)
else:
print ('quit!')
w=open('c:/python34/learn/data','w')
line='money:%d'%money+'\n'+'loan:%d'%loans
w.write(line)
w.close()
return
if __name__ == '__main__':
showmenu()
python核心程式設計9 10
9 10.家庭理財.建立乙個家庭理財程式.你的程式需要處理儲蓄,支票,金融市場,定期存款等多種帳戶.為每種帳戶提供乙個選單操作介面,要有存款,取款,借,貸等操作.另外還要提供乙個取消操作選項.使用者退出這個程式時相關資料應該儲存到檔案裡去 出於備份的目的,程式執行過程中也要備份.有兩天沒學寫程式了,...
python核心程式設計 練習題7 8
7 8.人力資源。建立乙個簡單的雇員姓名和編號的程式。讓使用者輸入一組雇員姓名和編號。你的程式可以提供按照姓名排序輸出的功能,雇員姓名顯示在前面,後面是對應的雇員編號。附加題 新增一項功能,按照雇員編號的順序輸出資料。用的python3.4 排序函式sorted整了半天 特別注意的是3.4對字典的排...
《Python核心程式設計》第10章 習題
10 6.改進的 open 為內建的 open 函式建立乙個封裝.使得成功開啟檔案後,返回檔案控制代碼 若開啟失敗則返回給呼叫者 none 而不是生成乙個異常.這樣你開啟檔案時就不需要額外的異常處理語句 def myopen infile,mode r try fo open infile,mode...