您的位置:首页 > 编程语言 > Python开发

python核心编程9-10

2015-10-22 08:42 661 查看
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: