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:])
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)
f=open('c:/python34/learn/data','r')
f.close()
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核心程式設計
文件字串 應該多用,help 的作用就是把builtin的函式的文件字串取出然後列印出來。1 usr bin python 2 def printmax a,b 3 print max of two int numbers 4 x int a 5 y int b 6 if x y 7 print x...
Python核心程式設計
略1 下劃線 在直譯器中有特別的含義,表示最後乙個表示式的值。2 print函式進行格式化字串 print s is number d python 1 python is number 1 3 重定向輸出 logfile open tmp mylog.txt a print logfile,fat...