Python

 

Introduction

This tutorial covers how to create a simple login and registration system using Python. The system allows users to register with a username and password, and then log in using those credentials. Basic Authentication is implemented using simple file handling to store user data.

Objective

The objective of this program is to teach you how to implement a basic authentication system with a user-friendly interface using Python. The application will feature:

  • User registration (username and password).
  • Login functionality to verify credentials.
  • Storing user data securely using Python file handling.

Code

# Basic Authentication System in Python

import os

# Function to register new user
def register_user():
    username = input("Enter username: ")
    password = input("Enter password: ")
    
    # Check if the user file exists
    if os.path.exists(f"{username}.txt"):
        print("Username already taken. Please choose another one.")
    else:
        # Create a new user file and save the password
        with open(f"{username}.txt", "w") as file:
            file.write(password)
        print("Registration successful!")

# Function to log in with an existing user
def login_user():
    username = input("Enter username: ")
    password = input("Enter password: ")

    # Check if the user file exists
    if os.path.exists(f"{username}.txt"):
        with open(f"{username}.txt", "r") as file:
            stored_password = file.read()
            if stored_password == password:
                print("Login successful!")
            else:
                print("Incorrect password. Try again.")
    else:
        print("Username not found. Please register first.")

# Main function
def main():
    print("Welcome to the Basic Authentication System!")
    
    while True:
        choice = input("\nEnter '1' for Register or '2' for Login or 'q' to Quit: ")
        
        if choice == '1':
            register_user()
        elif choice == '2':
            login_user()
        elif choice == 'q':
            print("Goodbye!")
            break
        else:
            print("Invalid option, please try again.")

# Run the main function
if __name__ == "__main__":
    main()

Program Structure and Explanation

The program consists of three main functions:

  1. register_user(): This function allows users to register by providing a username and password. It checks if the username is already taken, and if not, stores the password in a file named after the username.
  2. login_user(): This function allows users to log in by providing their username and password. It checks if the username exists, and if it does, compares the entered password with the stored password in the file.
  3. main(): The main loop of the program, where users can choose to register, log in, or quit. It continues running until the user chooses to quit by entering ‘q’.

How to Run the Program

To run this program:

    1. Ensure that Python is installed on your system.
    2. Save the code into a Python file, for example, auth_system.py.
    3. Open a terminal or command prompt, navigate to the directory where the file is located, and run the program using the following command:
python auth_system.py
  1. Follow the prompts to register and log in.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)