Management System in Python - #1 - Main Menu & Adding Users

Introduction: In this tutorial we will begin making a local user management system in Python. We have done this before in a simple SQLite database, but this series will be storing the information in simple plain text files ready for editing manually and portable safe. The Main System: First we want to create a main function which will be our menu system. The user will loop around this until they enter "0" to exit the program. This menu will also link directly in to other functions ready for the user to interact with...
  1. def main():
  2.     print("Welcome to my local file user management system [Created by Yorkiebar]. There are two options; 0=exit, 1=add user...\n");
  3.     going = 'y';
  4.     while (going is 'y'):
  5.         try:
  6.             user = int(input('Enter your option: '));
  7.         except ValueError:
  8.             print('That value is not legal. Please try again. 0=exit.');
  9.         if (user is 0):
  10.             going = '0';
  11.         elif (user is 1):
  12.             addUser();
As you can see, this function so far has two options; 0 to exit the program and 1 to add a user. The add user option calls a new function addUser. addUser Function: Now we need to create the addUser function. This function will first create a list of information, named information appropriately...
  1. def addUser():
  2.     information = [];
This list will be used to store all the information entered by the user for the new record to be added to the management system files. Now we want to create another loop. This time the user must enter the word "exit" to exit the loop
  1. def addUser():
  2.     information = [];
  3.     user = input('Enter information or "exit"... ');
  4.     while (user.lower() != 'exit'):
  5.         information.append(user);
  6.         user = input('Enter information or "exit"...');
So once the user has exited the loop, they are ready for the information to be added to the records file. So first we open a new stream to the file, with the file mode as append (since we only want to append to the bottom of the document and not overwrite the pre-exiting content). Then we want to move all the information stored in our information list in to a new string variable - a single line which will be added to the records document...
  1. def addUser():
  2.     information = [];
  3.     user = input('Enter information or "exit"... ');
  4.     while (user.lower() != 'exit'):
  5.         information.append(user);
  6.         user = input('Enter information or "exit"...');
  7.     print('Adding information to file...');
  8.     file = open('management.txt', 'a');
  9.     lineWrite = "";
  10.     for line in information:
  11.         lineWrite += line + "\t";
  12.     lineWrite += "\n";
Then finally we want to simply write the new line to the document and close the stream to the file - this avoids any reading/writing errors to/from the file later on and saves any syncing changes currently being made to the file. Once the function finishes, it automatically returns to the function all to continue with the main menu system ready for the user to enter a new option such as list or remove records...
  1. def addUser():
  2.     information = [];
  3.     user = input('Enter information or "exit"... ');
  4.     while (user.lower() != 'exit'):
  5.         information.append(user);
  6.         user = input('Enter information or "exit"...');
  7.     print('Adding information to file...');
  8.     file = open('management.txt', 'a');
  9.     lineWrite = "";
  10.     for line in information:
  11.         lineWrite += line + "\t";
  12.     lineWrite += "\n";
  13.     file.write(lineWrite);
  14.     print('Wrote information successfully.');
  15.     file.close();

Add new comment