Python core programming version 2, 405th page, Chapter 2 exercises continued 2-answers to Python core programming-self-developed-

Source: Internet
Author: User

This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.
Based on the reader's feedback or code review, I will update and supplement the answer or related content of this article, which will be added to the comments of this blog.
I try to ensure that the answer code for each question is complete, not just functions or classes. Open the Python 2.7 IDLE and copy the complete code to debug and run it.
Welcome to Balian's home http://www.cnblogs.com/balian at blog Park

13-4.
User Registration. Create a user database (including Logon Name, password, and last logon timestamp) class (see exercises 7-5 and exercises 9-12) to manage a system. This system requires that you can access certain resources only after logging on. This database class manages users, loads user information stored before the instantiation operation, and provides access functions to add or update database information. After the data is modified, the database saves the new information to the disk during garbage collection (see _ del __()).
[Note]
I use the accounts.txt file in the ddisk ex13_4folder to record the user name and password. There is only one user, Balian, And the password is apple. Use the login file userlog.txt to record the last logon timestamp. If the Balian logon is correct, the Last Logon Time and logon success information are returned. If the user name or password is incorrect, the system will prompt logon Failure and exit the program three times after logon. User management means that if login is successful, the password can be updated. The purpose is to make the code as simple as possible. After all, this is just an exercise.
Accounts.txt file content (only one line)
Balian 123456
Userlog.txt is an empty text file.
Prepare the preceding folders and files before running the program.

[Answer]
The Code is as follows:

#-*-Encoding: UTF-8-*-class DBMnagement (object): 'database management' def _ init _ (self, nm, pwd): self. name = nm self. password = pwd def updatePWD (self, newpwd): self. password = newpwd def _ del _ (self): pass # import osimport timedef CreateLog (username): 'logon successful write timeStamp to log file' timeStamp = time. ctime (time. time () log = 'user' + '-' + username + ', last time login Succeed at' + timeStamp + '\ n' logFile = open ('d: \ EX13_4 \ userlog.txt ', 'A') logFile. write (log) logFile. close () def ReadLog (): 'login successful. Read the last timestamp 'logfile = open ('d: \ EX13_4 \ userlog.txt ') logInfo = logFile from the log file. readlines () if len (logInfo)> = 1: print logInfo [-1] logFile. close () def ChangePWDinLog (username, newpwd): 'change user password' credentialFile = open ('d: \ EX13_4 \ accounts.txt ', 'w ') credentialtxt = username + ''+ newpwd credentialFile. write (credentialtxt) credentialFile. close () print 'password modified... password updated successfully. \ n' # The main program print 'starts from here. welcome to use the simplest user management system 'print' and try to log on with an existing user name and password in the system, you have three chances to log on to 'print' and enter the updatepwd command to change the password, after successful login, exit the system 'print'. After successful login, enter the quit command to exit the System \ n' credentialFile = open ('d: \ EX13_4 \ accounts.txt ') # obtain the username and password credentialInfo = credentialFile from the text file. readlines () credentialFile. close () usernameinDB = credentialInfo [-1]. split () [0] # Here credentialInfo [-1] is a string, including the username and password. passwordindm = credentialInfo [-1] separated by spaces. split () [1] errorTimes = 3 while errorTimes: print 'enter the user name and password: 'input_username = raw_input ('username...: ') input_password = raw_input ('password...: ') if input_username = usernameinDB and input_password = passwordindm: print': Login successful... login Successful 'CurrentUser = DBMnagement (input_username, input_password) ReadLog () CreateLog (input_username) input_command = raw_input (' enter your command, quit or updatepwd... \ n') if input_command = 'quit': del CurrentUser print '\ n has exited the system... logout successfully. 'break elif input_command = 'updatepwd ': newpwd = raw_input (' \ n enter the new password, new password...: ') CurrentUser. updatePWD (newpwd) ChangePWDinLog (CurrentUser. name, CurrentUser. password) del CurrentUser print '\ n has exited the system... logout successfully. 'break else: print' error command... wrong Command 'del CurrentUser print '\ n has exited the system... logout successfully. 'break else: print' incorrect user name or password... wrong Username or Password. \ n' errorTimes-= 1 if errorTimes = 0: print 'the system has exited... logout successfully.'

[Execution result]

You are welcome to use the simplest user management system to log on with an existing user name and password in the system. You have three chances to log on successfully and then enter the updatepwd command to change the password, after the system is successfully logged out, enter the quit command to exit the system. Enter the Username and password: Username...: balianPassword...: 123 incorrect username or password... wrong Username or Password. enter the Username and password: Username...: balianPassword...: 123456 incorrect username or password... wrong Username or Password. enter the Username and password: Username...: balianPassword...: apple login successful... login SuccessfulUser-balian, last time login Succeed at Wed Sep 12 12:32:35 2012 enter your command, quit or updatepwd... updatepwd enter the new password, new password...: The egg password has been modified... password updated successfully. logged out of the system... logout successfully.

[Reference]

Http://blog.csdn.net/lgfei/article/details/92044
How to Write text files in Python

Http://magustest.com/blog/softwaretesting/how-to-write-a-file-by-using-python/

Related Article

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.