Python Practice Program (c) function practice--ATM Simulation

Source: Internet
Author: User

Functional Requirements:

    • Quota 15000

    • Can withdraw, handling fee 5%

    • Record consumption flow

    • Support Monthly Billing Enquiry

    • Provide repayment interface

Related modules to invoke:

Import time #time模块, function time.strftime ("%y-%m-%d%x", Time.localtime ()) for various operation Times ' 2015-12-15 23:35:26 '
Import Getpass The method of entering the password at the command line #getpass module PWD = getpass.getpass (' Password: ') print pwd
Import Pickle #pickle模块, Serialize Python object # Save data to pickle file >>> dic = {}>>> import pickle>>> with Ope N (' Dic.pickle ', ' WB ') as F: ... pickle.dump (DIC, f) ... #从Pickle文件读取数据 >>> import pickle>>> with open (' Dic.pickle ', ' RB ') as F: .... dic = Pickle.load (f) ...

1. User data is serialized into Account.pickle by dictionary

Cat account.py #!/usr/bin/env python#coding=utf-8import Pickle account = {' 1001 ': {' name ': ' FGF ', ' PW ': 123, ' money ': 15000 , ' Balance ': 15000}, ' 1002 ': {' name ': ' User ', ' PW ': 123, ' money ': 15000, ' balance ': 15000}}f = File (' Account.pickle ', ' WB ') Pickle.dump (account,f) f.close () with open (' Account.pickle ', ' RB ') as F:dic = Pickle.load (f) f.close () Prin T DIC

2. Python Program

#!/usr/bin/env python#coding=utf-8import sys,pickle,getpass,time  #日志函数def  log (user, Describe,fee,balance,**userinfo):        time1 =  Time.strftime ("%y-%m-%d %x",  time.localtime ())         f  = file (' Account.log ', ' a ')         f.write ("%s      %s      %s      ¥%s       ¥%s\n " % (time1,user,describe,fee,balance))          f.close ()          f1 = file (' Account.pickle ', ' WB ')         pickle.dump (USERINFO,F1)          f1.close ()   #提现函数def  withdraw (user,**userinfo):         fee = int (raw_input("Please enter withdrawal Amount:"))         if fee % 100 ==0 and int (fee* 1.05)  <= userinfo[user][' balance ']:                 charge = fee * 0.05                 balance = userinfo[user][' balance ' ] - fee - charge                 userinfo[user][' Balance '] = balance                 log (User, "withdrawal (fee%d)"%charge,fee,balance,**userinfo )                 print  " You have successfully withdrawn%d, charge a handling fee%s, Remaining amount%s " % (fee,charge,balance)         else:                 print  "The amount entered is incorrect. "#还款函数def  repay (user,**usrinfo):         fee = int (raw_ Input ("Please enter repayment amount:"))         userinfo[user][' balance '] += fee         balance = userinfo[user][' Balance ']         log (User, "credit card repayment",-fee,balance,**userinfo)          print  "You have successfully repaid%d, current available quota%s"  % (fee,balance)   #账单查询def  bills (user):         f = file (' Account.log ', ' R ')          time = raw_input ("Please enter the time of query (format: YYYY-MM):")          match_yes = 0     #匹配标示          for line in f.readlines ():                if user in  line and time in line:                         print line,                          match_yes = 1        if  Match_yes == 0 : print "No query month Bill"  with open (' account.pickle ', ' RB ')  as f :         userinfo = pickle.load (f)          f.close () while true:        user =  raw_input ("\33[1;32;40m Please enter user name: \33[0m")         i = 1     &nBsp;   while userinfo.has_key (user):                 passwd = int (Getpass.getpass ("\33[1;32;40m Please enter password: \33[0m"))                 while  passwd == userinfo[user][' PW ']:                         i = 1                             print  "Welcome to the system, please select the operation:"                          choice  = int (Raw_input ("Withdrawal  1\t repayment  2\t Billing Enquiry  3\t  Quota enquiry  4\t  exit  0\n"))                           If   choice == 1: withdraw (User,**userinfo)                           Elif choice == 2: repay (User,**userinfo)                          elif  Choice == 3: bills (user)                          elif choice == 4:  print  "Total amount:", userinfo[user][' money '], "current quota", userinfo[user][' balance ']                          elif choice&nbsP;== 0: sys.exit ()                   else:                         i = i + 1                          print "Password Error! "                         if i == 4 : break          else : print  "does not have this account. "


This article is from the "Ling Feng" blog, please be sure to keep this source http://fgf01.blog.51cto.com/3907069/1726922

Python Practice Program (c) function practice--ATM Simulation

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.