Python之路06-信用卡
2020-12-13 02:43
标签:详情 login oca list() __name__ calendar dig sort 错误 需求: 1、信用卡消费 2、信用卡提现 3、信用卡还款 4、查看信用卡可用余额 5、打印账单 代码执行效果如下: Python之路06-信用卡 标签:详情 login oca list() __name__ calendar dig sort 错误 原文地址:https://www.cnblogs.com/hulk-1029/p/11050450.html# -*- coding: utf-8 -*-
import calendar, time, sys
import Login
reload(sys)
sys.setdefaultencoding(‘utf8‘)
def firstMenu():
print "请选择:"
print "1、信用卡消费"
print "2、信用卡提现"
print "3、信用卡还款"
print "4、查看当余额"
print "5、打印账单"
def listPrice():
‘‘‘计算信用卡总额‘‘‘
his_price = []
f = file(‘buy_list.txt‘)
for item in f.readlines():
price = float(item.split()[-1])
his_price.append(price)
f.close()
listPrice = sum(his_price)
return listPrice
def shopList():
‘‘‘打印购物list‘‘‘
shopList_dic = {}
print_dic = {}
#读取文件的内容写入字典
f = file(‘shopList.txt‘)
for item in f.readlines():
num = item.split()[0]
price = float(item.split()[2])
shopList_dic[num] = price
print_dic[num] = item
#将字典排序,排序后的值为list
list_sorted = sorted(print_dic.iteritems(), reverse = False)
#打印字典的值
for k,v in list_sorted:
print v,
f.close()
#返回值为列表
return list_sorted
def consumption( ):
‘‘‘信用卡消费‘‘‘
#将接收到的list转换为dict
shopList_dic = dict(shopList())
buy_list = []
price_list = []
while True:
buy_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
input = raw_input("请输入要购买的商品序号:").strip()
if shopList_dic.has_key(input):
buy_list.append(shopList_dic[input].split()[1])
price_list.append(float(shopList_dic[input].split()[-1]))
while True:
input = raw_input( "加入购物车成功,是否继续购物?yes/no\n").strip()
if input == ‘yes‘:
break
elif input == ‘no‘:
print "您购买的商品列表为:"
for i in buy_list:
print i,price_list[buy_list.index(i)]
print "您一共选择了%s件商品,合计%s元!" % (len(buy_list),sum(price_list))
#读取之前购物列表清单总价
allPrice = listPrice()+ sum(price_list)
if quota allPrice:
print "支付失败!您的信用卡额度为%s,您已经使用%s元!" % (quota,listPrice())
else:
print "购买成功,感谢本次购物!"
for i in buy_list:
list_str = buy_time + ‘\t‘ + i + ‘\t‘ + ‘¥‘ + ‘ ‘ + str(price_list[buy_list.index(i)]) + ‘\n‘
f = open("buy_list.txt", ‘a‘)
f.write(list_str)
f.close()
turnBack()
sys.exit(1)
else:
print "输入有误!"
else:
print "您购买的商品还没有哦,我们会尽快上架!"
def repayments():
‘‘‘信用卡还款‘‘‘
back_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
while True:
input = raw_input("请输入还款金额:").strip()
if input.isdigit():
input = 0 - float(input)
back_str = back_time + ‘\t‘ + ‘信用卡还款‘ + ‘\t‘ + ‘¥‘ + ‘ ‘ + str(input) + ‘\n‘
f = open("buy_list.txt", ‘a‘)
f.write(back_str)
f.flush()
f.close()
print("还款成功,可用余额为:")
listPrice()
break
else:
print "请输入有效数字!"
#返回上级
turnBack()
def Cash():
‘‘‘信用卡提现‘‘‘
cash_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
while True:
input = raw_input("请输入提现金额:").strip()
if input.isdigit():
only = quota - listPrice()*(1+0.05)
#print only
if float(input)%100 == 0 and float(input) only:
cash = float(input)*(1+0.05)
cash_str = cash_time + ‘\t‘ + ‘提现%s元‘ % input + ‘\t‘ + ‘¥‘ + ‘ ‘ + str(cash).split(‘.‘)[0] + ‘\n‘
f = open("buy_list.txt", ‘a‘)
f.write(cash_str)
f.flush()
f.close()
print "提现成功,请您注意查收!"
break
elif float(input)%100 != 0:
print "亲,提现只能是整百的哦!"
elif float(input) > only:
print "亲,您的提现超出限额了哦!"
else:
print "亲,请输入有效数字!"
#返回上级
turnBack()
def TIME():
‘‘‘获取当前月份月初日期和月末日期‘‘‘
day_now = time.localtime()
#day_begin = ‘%d-%02d-01‘ % (day_now.tm_year, day_now.tm_mon) # 月初肯定是1号
wday, monthRange = calendar.monthrange(day_now.tm_year, day_now.tm_mon) # 得到本月的天数 第一返回为月第一日为星期几(0-6), 第二返回为此月天数
day_end = ‘%d-%02d-%02d‘ % (day_now.tm_year, day_now.tm_mon, monthRange)
#print ‘月初日期为:‘,day_begin, ‘月末日期为:‘,day_end
return day_end
def printLitst():
‘‘‘月底打印账单‘‘‘
print "您的信用卡额度为%s,您已经使用%s元!" % (quota,listPrice())
last_day=TIME().split(‘-‘)[-1]
if now_day == last_day:
f = file(‘buy_list.txt‘)
buy_list = f.read()
print "本月账单详情如下:"
print buy_list
#将本月账单另存为文件
list_day = TIME()
mon_list = ‘%s_list.txt‘ % list_day
y = open(mon_list,‘w‘)
y.write(buy_list)
y.close()
#清空现有账单
f = open(‘buy_list.txt‘ , ‘w‘)
f.truncate()
f.close()
#os.system(‘> buy_list.txt‘)
#cp_cmd = ‘cp -r buy_list.txt buy_list`date "+%Y%m%d"`.txt‘
#SystemHelper.run_command(cp_cmd)
else:
pass
#print now_day,last_day
def overdue():
‘‘‘逾期未还款‘‘‘
if float(now_day) > 10 and listPrice() > 0:
overdue_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
overdue = listPrice()*0.05
overdue_str = overdue_time + ‘\t‘ + ‘逾期扣息‘ + ‘\t‘ + ‘¥‘ + ‘ ‘ + str(overdue) + ‘\n‘
f = open("buy_list.txt", ‘a‘)
f.write(overdue_str)
f.flush()
f.close()
print ‘逾期扣息:%s元‘ % overdue
#主菜单
def mainList():
print "welcome!!!"
firstMenu()
while True:
input = raw_input().strip()
if input == ‘1‘:
print "欢迎使用信用卡消费,以下是购物清单:"
consumption()
break
elif input == ‘2‘:
print "信用卡提现,手续费5%哦!"
Cash()
break
elif input == ‘3‘:
print "亲,终于要还款了!"
repayments()
break
elif input == ‘4‘:
print "您的信用卡额度为%s,您已经使用%s元!" % (quota,listPrice())
#返回上级
turnBack()
elif input == ‘5‘:
f = file(‘buy_list.txt‘)
buy_list = f.read()
print "本月账单详情如下:"
print "合计:",listPrice()
print buy_list
#返回上级
turnBack()
else:
print "输入有误,请重新输入!!"
#返回上级菜单
def turnBack():
print "1、返回上级"
print "2、退出"
while True:
input = raw_input("请选择:")
if input == ‘1‘:
mainList()
elif input == ‘2‘:
print "谢谢使用!"
sys.exit(1)
else:
print "输入有误,请重新输入!"
if __name__ == ‘__main__‘:
#信用卡额度为15000
global quota
quota = 15000
now_day = time.strftime(‘%d‘,time.localtime(time.time()))
#now_day = ‘30‘
# 登录验证,验证通过返回值为true
if Login():
print "您输入的用户名或密码错误已超3次,信用卡已锁定!!"
else:
#月底打印账单
printLitst()
#逾期扣款
overdue()
#主菜单
mainList()