Use Python3 to write simple credit card management programs _python

Source: Internet
Author: User
Tags auth flush sleep wrapper

1, program execution code:

#Author by Andy #_ *_ coding:utf-8 _*_ import os,sys,time base_dir=os.path.dirname (Os.path.dirname (__ 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 portal. ") Time.sleep (0.3) print (" 2), User login entry. "
  ") Print (" 3, exit Please press q! ") Choice=input (":") from core import, exit_flag=true while Exit_flag:user_choice=main.menu (choice) if use R_choice = ' 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 ("This feature is under construction, more refined Color, please look forward to! ") elif User_choice = = ' A ': Main.change_user_credit () elif User_choice = = ' B ': Main.add_user () Eli F User_choice = = ' C ': Main.del_user () elif User_choice = = ' d ': main.change_pwd () elif user_choice = ' Q ' or user_choice = ' Q ': Prin T ("Welcome to use again, goodbye!") ") Exit_flag = False

2, program function function:

#Author by andy#_*_ coding:utf-8 _* import json,sys,os,time,shutil base_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 for verification!"
    ") F = open (base_dir+ ' \data\\user_db.txt ', ' r ') log_file = open (base_dir+ ' \logs\log.txt ', ' + ', encoding= ' utf-8 ') Bill_log_file = open (Base_dir + ' \logs\\bill_log.txt ', ' + ', encoding= ' utf-8 ') Func_name = func.__name__ time_ formate = '%y-%m-%d%x ' start_time = Time.strftime (Time_formate, Time.localtime ()) User_data = Json.load (f) CO Unt=0 while Count < 3:global user_id global user_pwd user_id = input (' Please enter your card number: ') user_pwd = input (' Please enter your password: ') if user_id in user_data:if user_pwd = = user_data[user_id][' Password ']: Log_fi Le.write (start_time + ' card number%s certified successful!\n '% user_id) Log_file.flush () time.sleep (1) log_file.cl OSE keywords= 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 + ' initiated ' + func_name + ' business, Amount:%s '% keywords) Bill_log_file.flush () time.sleep (1) bill_log_file.close return keywords Els E: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 ("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 number%s has been locked for three consecutive validation failures!)
\ n '% user_id) time.sleep (1) log_file.close return wrapper #定义菜单函数, depending on the user to display the menu. Def menu (choice): if choice = = ' 2 ': Print ("Please select the service category: \ n" "1, check the credit limit.) \ n "" 2, credit card repayment. \ n "" 3, credit card present. \ n "" 4, modify password. \N "" 5, credit card transfer. \ n "" 6, credit card billing inquiries. \ 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 user credit Lines. \ n "" B, new credit card users. \ n "" C, remove credit card users. \ n "" D, modify user password. \ n "" "E, exit Please press q!\n") Service_items = input ('--> ') else:print ("Thank you for using, I wish a happy life!") ") exit () return Service_items # define backup user data file function def back_up_file (): time_formate = '%y-%m-%d ' sys_time = time.strf Time (Time_formate, Time.localtime ()) shutil.copy (Base_dir + "\data\\user_db.txt", Base_dir + "\data\\user_db--" + Sys_t IME + ". Bak.txt") #定义获取用户数据信息函数 def Get_user_data (): With open (Base_dir + "\data\\user_db.txt", ' r+ ', encoding= ' utf-8 ') s f:user_data = Json.load (f) return user_data #定义用户数据变量 user_data = Get_user_data () #定义查询信用额度函数 @auth def get_user_ Credit (): user_credit=user_data[user_id][' credits '] print ("You are currently in line:%s $ \ n"% (User_credit)) Time.sleep (2) Retu
 RN User_credit #定义信用卡还款函数 @auth def 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 are currently required to repay the amount:%s Yuan. \ n"%user_bill) exit_flag=true while Exit_
      Flag:repayment_value=input ("Please enter repayment amount:") if Repayment_value.isdigit (): Repayment_value=int (Repayment_value) user_data[user_id][' Balance '] = user_data[user_id][' Balance '] + repayment_value f = open (Base_dir + "\data\\use R_db.txt ", ' r+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print (" Congratulations, repayment success! ")
      ") Print (" The amount you currently need to repay is:%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!") ") #定义信用卡提现函数 @auth def enchashment (): user_credit=user_data[user_id][' credit '] print (" The amount of cash you can use is:%s "%user_credit) E Xit_flag=true while Exit_flag:enchashment_value=input ("Please enter the amount you want to withdraw:") if EnchaShment_value.isdigit (): Enchashment_value=int (enchashment_value) if enchashment_value% = = 0:if E Nchashment_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 ("Successful, your current available amount is:%s"%user_data[user_id][' Balance ']) time.sleep (1) exit_flag = False return Enchashment_value else:print ("Your amount of cash must be less than or equal to your credit limit!") Else:print ("The cash amount must be 100 integer times!"). "Else:print (" input is incorrect, the cash amount must be a number, and is 100 integer times ") @auth #定义信用卡转账函数 def transfer (): user_balance=user_data[user_id][' B Alance '] Print ("Your current available amount is:%s"%user_balance) exit_flag=true while exit_flag:transfer_user_id = input ("Please enter the account number:" Transfer_value = input ("Please enter transfer amount:") if transfer_user_id in User_data.keys (): While Exit_flag:if tr Ansfer_value.isdigit (): While Exit_flag:transfer_value=int (Transfer_value) user_passwd=input ("Please enter password to verify identity:")
              if user_passwd = = user_data[user_id][' Password ': user_balance = User_balance-transfer_value
              user_data[transfer_user_id][' Balance ']=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 ("Transfer successful, your current available amount is:%s"% user_balance) time.sleep (1) exit_flag = Fa LSE return Transfer_value Else:print ("Password error, please re-enter!") Else:print ("Transfer amount, must be a number, please confirm!") Else:print ("The account does not exist, please confirm!") ") # @auth #定义信用卡账单查询函数 @auth def billing_query (): Print (" We currently offer only 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 _log_file:if user_id in Lines:print (Lines.strip ()) print () time.sleep (1) #定义修改信用卡额度函数 def change_user_credit (): Print (" 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 (the new credits for card number%s is:%s)% (target_use R_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 (User_data, F) f.close () Prin T ("The credit limit has been revised successfully, the new limit has come into effect!") ") 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 ("Credit limit must be a number! Please confirm! Else:print ("card number does not exist, please confirm!") #定义修改口令函数 @auth def change_pwd (): Print (note: modifying user password!) ") Exit_flag = True while exit_flag:old_pwd = input (" Please enter current password: ") if old_pwd = = Get_user_data () [user_id][] Passw Ord "]: 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+ ', enc oding= ' Utf-8 ') json.dump (User_data, F) f.close () print ("Congratulations, password modification successful!") ") Time.sleep (1) exit_flag = False else:print (" Two times password inconsistent, please confirm! ") Else:print ("The password you entered is incorrect, please confirm it!") ") #定义新增信用卡函数 def add_user (): Exit_flag = True while exit_flag:user_id = input (" user_id: ") Balance = input (" B
      Alance: ") credit = input (" Credit: ") if Balance.isdigit () and Credit.isdigit (): Balance = Int (Balance) credit = Int (credits) else:print ("The balance and line 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, Na Me, Password)) choice = input ("Submit please press 1, cancel please press 2, exit please press Q:") if choice = = ' 1 ': Back_up_file () user_data=get_use R_data () user_data[user_id] = {"Balance": Balance, "credit": Credit, "name": Name, "Password": Password} f = O Pen (base_dir + "\data\\user_db.txt", ' w+ ', encoding= ' Utf-8 ') json.dump (User_data, F) f.close () print ("Add 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 (): E Xit_flag = True while Exit_flag:user_id=input ("Enter the card number of the credit card to be deleted:") if user_id = = ' Q ' or user_id = = ' Q ': Prin T (' 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][' Ba Lance '], user_data[user_id][' credit ', user_data[user_id][' Name ')) choice = input ("Submit please press 1, cancel please press 2, exit please press Q:") if CH Oice = = ' 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.close () print ("Delete user succeeded!") ") Time.sleep (1) exit_flag = False elif Choice = = ' 2 ': continue elif choice = = ' Q ' or Choice = = ' Q ': print (' Welcome to use again, goodbye! ') 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", "credit": 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", "credit": 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:

Login 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 launched repayment business, the amount is: 100
2016-12-20 21:27:01 Card number 005 launched repayment business, the amount is: 100
2016-12-20 22:14:20 Card number 005 launched repayment business, the amount is: 100
2016-12-20 22:17:26 card number 006 launched transfer business, the amount is: 300

The above is a small set to introduce the use of Python3 to write a simple credit card management procedures, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.