Building a Program to Read and Apply Rules Based on User Input

Introduction

In the world of programming, one of the fundamental tasks is to create programs that can process user input and provide appropriate responses. A common way to handle such scenarios is by defining a set of rules that guide the program’s behavior. This article will guide you through the process of developing a program that reads rules from a file and uses them to generate responses based on user input. This exercise will not only enhance your understanding of file handling in programming but also give you insight into rule-based systems.


Understanding the Problem

The task is to create a program that does the following:

  1. Read rules from a text file: The rules are predefined and stored in a text file.
  2. Accept user input: The program prompts the user to input some data.
  3. Apply the rules: The program then uses the rules read from the file to determine an appropriate response based on the user input.
  4. Respond accordingly: Finally, the program provides a response based on the rules and the user’s input.

Step-by-Step Guide

1. Creating the Rules File

Create a text file named rules.txt with three different situations. Here’s how you can define the rules in rules.txt file:

temperature=cold -> Wear a jacket.
temperature=hot -> Drink plenty of water.
day=monday -> Start the week with energy!
day=friday -> Enjoy your weekend ahead!
meal=breakfast -> Have a healthy start to your day.

In this file:

Each rule is structured as condition -> response.

The program will check user input against these conditions and provide the corresponding response.

2. Writing the Program

Here’s the Python code that reads the rules from the file and applies them to the user input:


def load_rules(filename):
    rules = {}
    with open(filename, 'r') as file:
    for line in file:
    if '->' in line:
    condition, response = line.strip().split(' -> ')
    rules[condition] = response
    return rules

def get_response(user_input, rules):
    for condition, response in rules.items():
    if condition in user_input:
    return response
    return "No matching rule found."

def main():
    rules = load_rules('rules.txt')
    user_input = input("Enter your input (e.g., 'temperature=cold'): ")
    response = get_response(user_input, rules)
    print("Response:", response)

if __name__ == "__main__":
    main()

3. Running the Program

Save the code in a file (e.g., rule_based_program.py) and ensure the rules.txt file is in the same directory.

Run the script and provide an input that matches one of the conditions in rules.txt.

Example Interactions:

Enter your input (e.g., 'temperature=cold'): temperature=cold
Response: Wear a jacket.

Enter your input (e.g., 'day=monday'): day=monday
Response: Start the week with energy!

Enter your input (e.g., 'meal=breakfast'): meal=breakfast
Response: Have a healthy start to your day.

Conclusion

This exercise demonstrates the power and flexibility of rule-based programming. By storing rules in a file, you can easily modify the program's behavior without changing the code itself. This approach is widely used in developing decision-making systems, automated response systems, and more. Experiment with different scenarios to explore the versatility of this method!


Comments