Python Tutorials: Creating a Simple Python AI

About: 1 new python tutorial every day Comment what you want the next one to be Please favourite and subscribe Notice Board: - since instructables has automatically been removing my indentations from this point o...

Intro: Python Tutorials: Creating a Simple Python AI

Step 1: The Code

After opening a blank python file, type the following code:

--- = tab

while True:
---userInput = input(">>> ")
---if userInput in ['hi', 'HI', 'Hi']:
------print("Hello")
---else:
------print("I did not understand what you said")



This can be extended with elif statements and eventually you will eventually have your very own slightly clever AI. Just make sure that the else statement is always at the bottom of your code

Share

    Recommendations

    • Tiny Home Contest

      Tiny Home Contest
    • Furniture Contest 2018

      Furniture Contest 2018
    • Metalworking Contest

      Metalworking Contest

    35 Discussions

    0
    None
    reece.noone

    2 months ago

    Good first Python tutorial, but this isn't an AI. It is a computer program. The program is not intelligent, it is just a procedural program. I may recommend changing the title to "Introduction to Python" because it is misleading and may be giving people misinformation.

    To be an AI, the program would have to learn to do something and by that it, I mean you cant just program it using if statements to compare an input. Artificial Intelligence uses, normally, reinforcement learning to learn, in the same way you train a dog. There isn't a 'simple' AI.

    Please, in a sector of technology that is already confusing, don't create more confusion.

    0
    None
    cj_disabledVet

    Question 3 months ago on Step 1

    python 3.7 SyntaxError: invalid syntax

    Python 3.7.0b5 (v3.7.0b5:abb8802389, May 30 2018, 20:03:19)

    [Clang 6.0 (clang-600.0.57)] on darwin

    Type "copyright", "credits" or "license()" for more information.

    >>> --- = tab

    while True:

    ---userInput = input(">>> ")

    ---if userInput in ['hi', 'HI', 'Hi']:

    ------print("Hello")

    ---else:

    ------print("I did not understand what you said")

    SyntaxError: invalid syntax

    >>>

    0
    None
    Clavallee

    Question 4 months ago on Step 1

    --- = tab

    Receive invalid syntax

    0
    None
    Java9Pro

    1 year ago

    Is this AI programming?...not even basic :)

    0
    None
    PatriciaL112

    1 year ago

    This is a pretty simple starting example that can be used for beginning programmers who probably haven't created a single line of code and want to get into AI programming. Of course it is hardcoded but I'd place that under the weakest of AI because simply put, AI is fake intelligence. The whole point is to simulate some kind of intelligence that we humans portray when we communicate. It's diificult to get a computer with no built in and innate sense of common sense or consciousness to actually when attain those traits. We are humans, not freaking gods or mother nature. But we've come pretty far in AI and I hope we can get even farther even with the protests of the supposed "dangers of AI".

    0
    None
    GANESHY

    2 years ago

    I think you dont know what actually AI is

    0
    None
    adilawan62

    2 years ago

    your all melons

    0
    None
    MichaelB509

    2 years ago

    This is a decent beginner tutorial. But, you should of used "if userinput == "hi".lower(): # do stuff " its less work. If you wanted to use a list what I would of done is"input = ["hi"] if userinput.lower() in input: # do code"

    2 replies
    0
    None
    BlueBlood StudiosMichaelB509

    Reply 2 years ago

    this is an oldish tutorial, I had not realised .lower() existed in the syntax of Python. As for your other comment, this isn't an AI since it's not learning anything. it's more about giving people an idea of how to use if statements and if else, etc. I have a more advanced version of an AI here: https://www.pythonanywhere.com/shared_console/83e3371d-bbfa-4f66-9ccd-3cd0372f5b17 this one learns from what the user types in. This one technically qualifies as an AI as it learns from what the user types in. it also has the ability to create opinions of you ask it an opinion (e.g if you ask it a question like 'do you like chocolate?' it will generate an opinion and that opinion will stay the same) I did plan to advertise this version more and see if I could get many people to use it, therefore speeding up the learning process

    0
    None
    MichaelB509BlueBlood Studios

    Reply 2 years ago

    That is kinda cool but you should look into something like pyymal or nltk. But its cool :)

    0
    None
    MichaelB509

    2 years ago

    Also, this isn't what I'd consider an A.I.

    0
    None
    JohnS380

    2 years ago

    It's a very nice program for beginners, but I have to say, if you don't know anything about python, please don't try making any AI program, and then fill the comments section with questions that have an obvious answer. Codecademy has a great python tutorial, for Python 2.7. The program here is in python 3, but changes are minor(print became a function and raw_input does not exist, and some other that are more advanced). Anyways, that's all I had to say, so please take some time and learn the language if you are really interested.

    1 reply
    0
    None
    BlueBlood StudiosJohnS380

    Reply 2 years ago

    generally people prefer Python 2.7 due to it having more compatible external libraries. However I personally prefer Python 3 as I started learning the language a few months after the release of it. the main differences to basic Python is using raw_input instead of input in Python 2 and the difference in syntax with the print command

    0
    None
    MaxC3

    3 years ago on Introduction

    Neither of the codes are working for me. The console says

    >>> hi

    Traceback (most recent call last):

    File "Monty1.py", line 2, in <module>

    NameError: name 'hi' is not defined

    Press any key to continue...

    2 replies
    0
    None
    JohnS380MaxC3

    Reply 2 years ago

    Change the " userInput = input(">>> ") " line to " userInput = str(input(">>> ")) "

    This will make the user input a string, and it will be accepted. Otherwise python thinks that you asked for a variable.

    0
    None
    YOLOswaggins24MaxC3

    Reply 3 years ago

    Also, make sure your using 'raw_input' and not just 'input'.