Free online Python course for kids - Programming for Beginners

Employee Database

Session 9: Write an Employee Database for the Animal Shelter

Course Overview | Previous Session | Next Session

Introduction

In today’s session, we’re going to help the animal shelter manage its employee data using Python. Instead of focusing on coding from scratch, we’ll learn how to use Python to interact with data stored in a .txt file. This session is about using existing Python code to work with data and will provide you with tools to look up specific information as needed.

Create a Text File

  • Open your text editor.
  • Copy and paste the following employee data:
Jack Bossinger;Shelter Manager;555-1234
Susan Boothe;Volunteer Coordinator;555-5678
Bob Johnson;Animal Caretaker;555-8765
Emily Davis;Fundraising Coordinator;555-4321
Michael Lee;Veterinary Technician;555-6789
  • Save this file as employees.txt in your code directory.

Create the Python Script

  • Open your text editor and create a new file named fetchEmployee.py.
  • Copy and paste the following code into fetchEmployee.py:
# fetchEmployee.py

import sys

def fetch_employee(index):
    try:
        index = int(index)
        with open('employees.txt', 'r') as file:
            lines = file.readlines()
            
            if index < 1 or index > len(lines):
                print(f"Error: Index {index} is out of range. There are only {len(lines)} employees.")
                return
            
            # Fetch the line at the specified index (adjusted for 0-based index)
            line = lines[index - 1].strip()
            name, position, phone = line.split(';')
            print(f"Name: {name}")
            print(f"Position: {position}")
            print(f"Phone: {phone}")
    
    except ValueError:
        print("Error: The index must be an integer.")
    except FileNotFoundError:
        print("Error: The file 'employees.txt' does not exist.")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python fetchEmployee.py <index>")
    else:
        fetch_employee(sys.argv[1])
  • Save the file in the same directory as employees.txt.

Open Your Terminal

  • Make sure your terminal (or command prompt) is open.Navigate to the directory where employees.txt and fetchEmployee.py are saved.

Run the Python Script

  • To fetch information about a specific employee, type the following command:

Replace <index> with the number of the employee you want to look up. For example, to fetch information about the 3rd employee:

Check the Output

  • After running the script, you will see details about the specified employee. For the example above, you would see:

In this session, you’ve learned how to use Python to interact with data from a text file. This approach allows you to query specific information without modifying the script each time. While today’s focus is on using Python to work with data, remember that Python can also be used to create graphical user interfaces, generate charts, and much more, which will be covered in more advanced courses.

Exercise Suggestions

  • Experiment with Your Data: Modify the employees.txt file to include new data or change existing entries. Use the script to query different employees.
  • Create Your Own Data Files: Try creating and querying other types of data files, like a book index or song list, using a similar approach.

Keep practicing and exploring different ways to use Python to manage and interact with data!


Course Overview | Previous Session | Next Session