Write a simple credit card management program using Python3

Source: Internet
Author: User
This article mainly introduces the use of Python3 to write a simple credit card management program code, very good, with reference value, need to refer to a friend

1. Program Execution Code:

#Author by Andy#_*_ coding:utf-8 _*_import os,sys,timebase_dir=os.path.dirname (Os.path.dirname (Os.path.abspath (__ file__)) Sys.path.append (Base_dir) str= "Welcome to the bank credit card self-service system! \ n "For I in Str:sys.stdout.write (i) Sys.stdout.flush () Time.sleep (0.3) while True:print (" 1, administrative personnel entrance. ") Time.sleep (0.3) print (" 2, User login entry.  ") Print (" 3, exit Please press q! ") Choice=input (":") from core import main exit_flag=true while Exit_flag:user_choice=main.menu (choice) if User_cho      Ice = = ' 1 ': Main.get_user_credit () elif User_choice = = ' 2 ': main.repayment () elif User_choice = = ' 3 ':    Main.enchashment () elif User_choice = = ' 4 ': main.change_pwd () elif User_choice = = ' 5 ': Main.transfer () elif User_choice = = ' 6 ': Main.billing_query () elif user_choice = ' 7 ': Print ("The feature is under construction, more exciting, please look forward! ") elif User_choice = = ' A ': Main.change_user_credit () elif User_choice = = ' B ': Main.add_user () elif use     R_choice = = ' C ': Main.del_user () elif User_choice = = ' d ': Main.change_pwd () elif User_choice = = ' Q ' or user_choice = = ' Q ': print ("Welcome to use again, goodbye!") ") Exit_flag = False

2, program function function:

#Author by Andy#_*_ coding:utf-8 _*import json,sys,os,time,shutilbase_dir=os.path.dirname (Os.path.dirname ( Os.path.abspath (__file__)) sys.path.append (Base_dir) #定义认证装饰器def auth (func): Def wrapper (*args,**kwargs): # Print (" Please enter the card number and password to verify!    ") F = open (base_dir+ ' \data\\user_db.txt ', ' r ') log_file = open (base_dir+ ' \logs\log.txt ', ' A + ', encoding= ' utf-8 ') Bill_log_file = open (Base_dir + ' \logs\\bill_log.txt ', ' A + ', encoding= ' utf-8 ') Func_name = func.__name__ Time_forma    Te = '%y-%m-%d%x ' start_time = Time.strftime (Time_formate, Time.localtime ()) User_data = Json.load (f) count=0 While Count < 3:global user_id global user_pwd user_id = input (' Please enter your card number: ') user_pwd = input (' Please enter your The password: ') if user_id in user_data:if user_pwd = = user_data[user_id][' Password ']: Log_file.write (start_t IME + ' card number%s certified successful!\n '% user_id) Log_file.flush () time.sleep (1) log_file.close Keywor     ds = Func (*args, **kwargs)     if func_name = = ' repayment ' or func_name = = ' transfer ' or func_name = = ' Enchashment ': Bill_log_file.write (            Start_time + ' card number ' + user_id + ' initiate ' + func_name + ' business, amount:%s \ n '% keywords) bill_log_file.flush ()        Time.sleep (1) bill_log_file.close return keywords Else:return keywords Else:print (' Card number or password error! Please re-enter!          ') log_file.write (start_time + ' card number%s authentication failed!\n '% user_id) Log_file.flush () time.sleep (1) Log_file.close Count +=1 else:print ("The card number does not exist, please confirm!") if Count = = 3:print ("Sorry, you have lost 33 times, the card number is locked!") ") Log_file.write (start_time + ' card '%s ') was locked for three consecutive verification failures! \ n '% user_id) time.sleep (1) log_file.close return wrapper# defines the menu function, which displays the non-pass menu according to different users. Def menu (choice): if choice = = ' 2 ': Print ("Please select service category: \ n" "1, check the credit limit.) \ n "" 2, credit card repayment. \ n "" 3, credit card withdrawal. \ n "" 4, Change password. \ n "" 5, credit card transfer. \ n "" 6, credit card billing enquiry. \ n "" 7, easy shopping. \ n"8, Exit Please press q!\n") Service_items = input ('--') elif choice = = ' 1 ': Print ("Please select service category: \ n" "A, modify the user credit limit.) \ n "" B, add credit card users. \ n "" C, delete the credit card user. \ n "" D, modify the user password. \ n "" E, exit Please press q!\n ") Service_items = input ('---') else:print (" Thanks for using, Wish life happy! ") Exit () Return service_items# define the backup user data file function def back_up_file (): time_formate = '%y-%m-%d ' sys_time = Time.strftime (T Ime_formate, Time.localtime ()) shutil.copy (Base_dir + "\data\\user_db.txt", Base_dir + "\data\\user_db--" + Sys_time + ". Bak.txt ") #定义获取用户数据信息函数def Get_user_data (): With open (Base_dir +" \data\\user_db.txt ", ' r+ ', encoding= ' Utf-8 ') as F:use R_data = Json.load (f) Return user_data# define user data variables User_data = Get_user_data () #定义查询信用额度函数 @authdef get_user_credit (): User_ credit=user_data[user_id][' Credit ' print ("Your current limit is:%s yuan \ n"% (User_credit)) Time.sleep (2) Return user_credit# define credit card Also Section function @authdef repayment (): User_data = Get_user_data () user_credit=int (user_data[user_id][' credit ') User_balance=int ( User_data[user_id][' Balance ') User_bill = user_credit-user_balance print ("You currently need to repay the amount:%s Yuan. \ n"%user_bill) Exit_flag=true W Hile exit_flag:repayment_value=input ("Please enter repayment amount:") if Repayment_value.isdigit (): Repayment_value=int (repayment_v alue) user_data[user_id][' Balance '] = user_data[user_id][' Balance '] + repayment_value f = open (Base_dir + "\data \\user_db.txt ", ' r+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print (" Congratulations, the repayment is successful! ")      Print ("You currently need to repay the amount:%s Yuan. \ n"% (user_data[user_id][' credit ')-user_data[user_id][' Balance ')) Time.sleep (1) Exit_flag = False return repayment_value else:print ("Please enter the correct amount!")  ") #定义信用卡提现函数 @authdef enchashment (): user_credit=user_data[user_id][' credit '] print (" Your available withdrawal quota is:%s "%user_credit) Exit_flag=true while Exit_flag:enchashment_value=input ("Please enter the amount you want to cash in:") if Enchashment_value.isdigit (): Enchash Ment_value=int (enchashment_value) if enchashment_value% = = 0:if Enchashment_value <= user_credit:user_data[user_id][' Balance '] = user_credit-enchashment_value f = open (Base_ Dir + "\data\\user_db.txt", ' r+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print ("withdrawal Success, your current available quota is:%s "%user_data[user_id][' Balance ')" Time.sleep (1) exit_flag = False return Enchash Ment_value Else:print ("Your withdrawal amount must be less than or equal to your credit limit!") ") Else:print (" The cash amount must be an integral multiple of 100!) ") Else:print (" The input is incorrect, the cash amount must be a number, and an integer multiple of 100 ") @auth # define the credit card transfer function Def transfer (): user_balance=user_data[user_id][' balance Print ("Your current available quota is:%s"%user_balance) exit_flag=true while exit_flag:transfer_user_id = input ("Please enter the offset account:") trans Fer_value = input ("Please enter the transfer amount:") if transfer_user_id in User_data.keys (): While Exit_flag:if transfer_value.i Sdigit (): While Exit_flag:transfer_value=int (Transfer_value) user_passwd=input ("Please enter a password to verify the identity : ") if user_passwd = = user_data[user_id][' Password ': user_balance = user_balance-transfer_value user_data[transfer_user_id][' B Alance ']=int (user_data[transfer_user_id][' Balance ') + transfer_value f = open (Base_dir + "\data\\user_db.txt ", ' r+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print (" The transfer is successful, your current available quota is:            %s "% user_balance) time.sleep (1) exit_flag = False return transfer_value Else:print ("Password is wrong, please re-enter!") ") Else:print (" The transfer amount must be a number, please confirm! ") ") Else:print (" The account does not exist, please confirm! ") # @auth # define the credit Card bill query function @authdef billing_query (): Print ("We currently only offer to query all billing features!") Print ("Your bill is: \ n") Bill_log_file = open (Base_dir + ' \logs\\bill_log.txt ', ' R ', encoding= ' utf-8 ') for lines in Bill_lo G_file:if user_id in Lines:print (Lines.strip ()) print () time.sleep (1) #定义修改信用卡额度函数def change_user_credit (): P Rint ("You are modifying the user's credit limit!") ") Exit_flag=true while EXIT_FLAG:TARGET_USER_ID=INPUT ("Please enter the user card number you want to modify: \ n") if target_user_id in User_data.keys (): While Exit_flag:new_credit=input ("Please enter a new credit limit: \ n ") if New_credit.isdigit (): new_credit= int (new_credit) user_data[target_user_id][' credit ']=new_          Credit print ("New credits for card number%s:%s"% (target_user_id,new_credit)) choice = input ("Confirm please enter 1 or press any key to cancel: \ n") if choice = = ' 1 ': F = open (Base_dir + "\data\\user_db.txt", ' r+ ', encoding= ' Utf-8 ') json.dump (US Er_data, F) f.close () print ("The credit limit has been successfully modified and the new limit has been applied!")            ") print (the new credit limit for card number%s is:%s"% (target_user_id, user_data[target_user_id][' credits ')) Time.sleep (1) Exit_flag = False Else:print ("The user's credit limit has not changed! Else:print ("The credit limit must be a number!") Please confirm! ") Else:print (" The card number does not exist, please confirm! ") ") #定义修改口令函数 @authdef change_pwd (): Print (" Note: The user password is being modified! ") Exit_flag = True while exit_flag:old_pwd = input (" Please enter current password: ") if old_pwd = = Get_user_data () [user_id][] PAssword "]: New_pwd = input (" Please enter new password: ") New_ack = input (" Please enter new password again: ") if new_pwd = = New_ack:user_data= Get_user_data () user_data[user_id]["Password"]=new_pwd f = open (Base_dir + "\data\\user_db.txt", ' r+ ', encod ing= ' Utf-8 ') json.dump (User_data, F) f.close () print ("Congratulations, password modification succeeded!") ") Time.sleep (1) exit_flag = False else:print (" Two times the password is inconsistent, please confirm! ") ") Else:print (" The password you entered is not correct, please confirm it! ") ") #定义新增信用卡函数def Add_user (): Exit_flag = True while exit_flag:user_id = input (" user_id: ") Balance = input (" Balance : ") credit = Input (" Credits: ") if Balance.isdigit () and Credit.isdigit (): Balance = Int (Balance) credit = i NT (Credit) Else:print ("balance and credits must be numbers!")       ") Continue Name = input (" name: ") Password = input (" Password: ") print (" New credit card user information is: \ n "" user_id:%s\n " "Balance:%s\n" "credit:%s\n" "name:%s\n" "password:%s\n"% (user_id, Balance, Credit, Name, Pa ssWOrd)) CHoice = input ("Submit please press 1, cancel please press 2, exit please press Q:") if choice = = ' 1 ': Back_up_file () user_data=get_user_data () User_data [user_id] = {"Balance": Balance, "credits": Credit, "name": Name, "Password": Password} f = open (Base_dir + "\data\\us Er_db.txt ", ' w+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print (" New User success!      ") Time.sleep (1) exit_flag=false elif choice = = ' 2 ': continue elif choice = = ' Q ' or choice = = ' Q ': Time.sleep (1) exit_flag = False else:print (' invaliable options! ') #定义删除信用卡函数def Del_user (): Exit_flag = True while Exit_flag:user_id=input ("Enter the card number of the credit card you want to delete:") if user_id = = ' Q ' or u ser_id = = ' Q ': print (' Welcome to use again, bye! ') Time.sleep (1) exit_flag=false else:user_data=get_user_data () print ("New credit card user information is: \ n" "Use r_id:%s\n "" balance:%s\n "" credit:%s\n "" name:%s\n "% (user_id, user_data[user_id][' Balanc E '], user_data[user_id][' credit '], User_data[user_id][' Name '])) choice = input ("Submit press 1, cancel please press 2, exit please press Q:") if choice = = ' 1 ': Back_up_file () User_data.pop ( USER_ID) F = open (Base_dir + "\data\\user_db.txt", ' w+ ', encoding= ' Utf-8 ') json.dump (User_data, F) F.C Lose () print ("Delete user succeeded! ") Time.sleep (1) exit_flag = False elif Choice = = ' 2 ': continue elif choice = = ' Q ' or choic E = = ' Q ': print (' Welcome to use again, bye! ') Time.sleep (1) exit_flag = False else:print (' invaliable options! ')

3. User Data files:

{"003": {"name": "Wangwu", "Password": "Qazwsx", "credit": 16000, "Balance": 8000}, "004": {"name": "Zhaoliu", "Password": "EDCRFV", "credit": 18000, "Balance": 6000}, "002": {"Name": "Lisi", "Password": "123456", "Credits": 14000, "Balance": 10 "009": {"Password": "qwerty", "name": "Hanmeimei", "credit": 15000, "Balance": 15000}, "005": {"Name": "Fengqi", "Pa ssWOrd ":" 1234qwer "," credit ": 15000," Balance ": 10700}," 010 ": {" Name ":" Lilei "," Password ":" qaswed "," credits ": 50000," Balance ": 50000}," 008 ": {" name ":" Zhengshi "," Password ":" 123456 "," credit ": 12345," Balance ": 12345}," 006 ": {" name ":" Z Houba "," Password ":" 123456 "," credit ": 20000," Balance ": 8300}," 001 ": {" Name ":" Zhangsan "," Password ":" abcd1234 "," Cred It ": 12000," Balance ": 12000}," 007 ": {" Name ":" Wujiu "," Password ":" 123456 "," credit ": 20000," Balance ": 11243}}

4. Related Log content:

Log in log:

2016-12-20 22:12:18 Card number 005 Certification success!
2016-12-20 22:14:20 Card number 005 Certification success!
2016-12-20 22:17:26 Card number 006 Certification success!
2016-12-20 22:18:06 Card number 005 Certification success!
2016-12-20 22:18:06 Card number 006 Certification success!
2016-12-20 22:21:10 Card number 005 certification failed!
2016-12-20 22:21:10 Card number 006 Certification success!
2016-12-20 22:23:17 Card number 006 Certification success!
2016-12-20 22:25:33 Card number 006 Certification success!
2016-12-20 22:26:14 Card number 006 Certification success!
2016-12-20 22:32:15 Card number 006 Certification success!
2016-12-20 22:44:57 Card number 005 Certification success!
2016-12-20 22:45:50 Card number 006 Certification success!
2016-12-20 22:47:10 Card number 006 Certification success!
2016-12-20 22:48:27 Card number 006 Certification success!
2016-12-20 22:49:30 Card number 006 Certification success!
2016-12-20 22:52:13 Card number 006 Certification success!
2016-12-20 22:53:44 Card number 006 Certification success!

Transaction log:

2016-12-20 21:25:35 card number 006 launches repayment business, the amount is: 100
2016-12-20 21:27:01 Card number 005 launches repayment business, the amount is: 100
2016-12-20 22:14:20 Card number 005 launches repayment business, the amount is: 100
2016-12-20 22:17:26 card number 006 launches transfer business, the amount is: 300

The above is a small part of the introduction of the use of Python3 to write a simple credit card management procedures, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.