Python Tutorials: Creating a Simple Python AI

Published

Introduction: 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...

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

    • Water Contest

      Water Contest
    • Oil Contest

      Oil Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    35 Discussions

    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.

    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

    >>>

    --- = tab

    Receive invalid syntax

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

    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".

    I think you dont know what actually AI is

    your all melons

    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

    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

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

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

    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

    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

    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

    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.

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