Artificial Intelligence (AI) Python Code Examples

#1 A simple example of a Python code using a basic form of artificial intelligence

Here is a straightforward example of Python code that utilizes a basic form of artificial intelligence, specifically a simple decision-making system. Our goal with the program is to determine whether a person is eligible to vote based on age.

Related: AI Seminar Topics

def check_voting_eligibility(age):
    if age >= 18:
        return "You are eligible to vote!"
        return "Sorry, you are not eligible to vote yet."

# Get user input for age
user_age = int(input("Enter your age: "))

# Check eligibility using the AI decision-making system
result = check_voting_eligibility(user_age)

# Display the result

The check_voting_eligibility function serves as a basic decision-making system. It receives the user’s age as input and determines whether they can vote based on the condition (age >= 18). The program utilizes AI logic to ascertain and print the eligibility status.

#2 A basic rule-based system to simulate a chatbot that can respond to user inputs.

Here’s an example of a simple chatbot using basic rule-based system to simulate a conversation with users. This example showcases artificial intelligence in the context of natural language processing without involving machine learning.

import random

def simple_chatbot(user_input):
    greetings = ["Hello!", "Hi there!", "Hey!", "Greetings!"]
    farewells = ["Goodbye!", "See you later!", "Bye!", "Farewell!"]
    questions = ["How are you?", "What's your name?", "Where are you from?"]

    user_input = user_input.lower()

    if any(word in user_input for word in ["hello", "hi", "hey"]):
        return random.choice(greetings)
    elif "goodbye" in user_input:
        return random.choice(farewells)
    elif any(word in user_input for word in ["how", "what", "where"]):
        return random.choice(questions)
        return "I'm not sure how to respond to that."

# Example usage
while True:
    user_input = input("You: ")
    if user_input.lower() == "exit":
        print("Chatbot: Goodbye!")
    response = simple_chatbot(user_input)
    print("Chatbot:", response)

The function called simple_chatbot is designed to take user input and provide responses based on specific rules that have been predefined. The chatbot has basic patterns that detect greetings, farewells, and questions. The chatbot will respond appropriately if the user input matches these patterns. Otherwise, it will provide a generic response. You can make this chatbot more interactive by adding rules and responses.

Artificial intelligence is a vast field involving complex technologies such as machine learning models, neural networks, and advanced algorithms, depending on the task. However, these basic examples can help grasp the decision-making concept in artificial intelligence using Python.

Related Creative Project ideas