Introduction: Python Tutorials: Creating a Simple Python AI

Picture of Python Tutorials: Creating a Simple Python AI

Step 1: The Code

Picture of 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

Comments

Java9Pro (author)2017-09-16

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

PatriciaL112 (author)2017-08-30

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

dchakraborty5 (author)2016-08-07

This isn't an AI. This is hard-coded.

Raphango (author)dchakraborty52017-05-24

Actually agree... =T

AI is a far more complicated subject....

Take a look at this:

http://letzgro.net/blog/creating-ai-using-python/

=)

GANESHY (author)2016-07-26

I think you dont know what actually AI is

adilawan62 (author)2016-04-29

;)

adilawan62 (author)2016-04-29

your all melons

adilawan62 (author)2016-04-29

cool

MichaelB509 (author)2016-03-13

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"

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 :)

MichaelB509 (author)2016-03-13

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

JohnS380 (author)2016-01-06

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.

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

MaxC3 (author)2014-12-23

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

JohnS380 (author)MaxC32016-01-06

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.

YOLOswaggins24 (author)MaxC32015-06-09

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

my friend, this is a python 3 tutorial so raw_input does not exist

YOLOswaggins24 (author)MaxC32015-06-09

Male sure 'Hi' is in quotations. It needs to be a string...

pycharm (author)MaxC32014-12-25

while True:

--->userInput = raw_input('>>> ')

--->if userInput.lower() in ['hi', 'hello', 'good morning', 'good evening']:

--->--->print ('Hello there')

--->--->break

--->else:

--->--->print (userInput.lower(), type(userInput))

---> = single indent

--->---> = double indent

TristanS4 (author)2015-08-04

slade101 (author)2015-07-18

none of these worked for me

by none of them do you mean none of the tutorials

LightB (author)2015-05-22

its a great instructable, however, for the python beginners like myself.....is there a version without pseudocode? that stuff works when you know how to implement a feature, and if you're the kind of person that learns from things that are already built. its an extra 3 hours :) cool idea though, i always thought that if statements were the center of an ai

i think....you used a while command to give the if a condition?

YOLOswaggins24 (author)LightB2015-06-09

He used the 'while' statement to cause the program to repeatedly ask for input over and over again.

LightB (author)LightB2015-05-22

that is true, i learn most code by copy pasting and seeing how its laid out.

python is, a bit weird in that if you copy paste, it doesn't always follow the white-space, and i still sometimes forget what to use. and where.

my mistake :)

BlueBlood Studios (author)LightB2015-05-22

copying and pasting is the worst way to learn as your brain is not actually interpreting what you do, if you wish to begin learning Python I would recommend investing in this book: http://www.amazon.co.uk/Python-Programming-Absolute-Beginner-Dawson/dp/1435455002 it's what first got me into programming. I was planning on stopping the Python tutorial series but I may start it up again to give you a hand with learning it

BlueBlood Studios (author)LightB2015-05-22

I think you may be confused about what pseudocode is. pseudocode is writing out what you are planning to do pretty much in plain English which basic Python, as it is such a high level language, is already very similar to

LightB (author)2015-05-22

pycharm (author)2014-12-25

That is a good start. Here is my 2 cents for the friends who are using Python 2.X. Use the function 'raw_input' instead of 'input'.

'input' evaluates the user input to give a result. If the input is syntactically wrong it can not be evaluated so it gives an error message.

eg.

userInput = input('>>> ')

print userInput

Execute the above two lines of code.

Trial 1

>>>1+2 (this is a user input)

3 (the user input was evaluated to produce this result)

Trial 2

>>>hi (this is another user input, which is syntactically wrong for python)

Traceback (most recent call last):

File "<pyshell#5>", line 1, in <module>

input(hi)

NameError: name 'hi' is not defined (the error message the user gets)

Read more about 'input' here https://docs.python.org/2/library/functions.html#i...

Now the alternate version of this instructable is here.

while True:

--->userInput = raw_input('>>> ')

--->if userInput.lower() in ['hi', 'hello', 'good morning', 'good evening']:

--->--->print ('Hello there')

--->--->break

--->else:

--->--->print (userInput.lower(), type(userInput))

---> = single indent

--->---> = double indent

Jan-Hendrik Ewers (author)2014-12-03

This would keep going over and over as the while loop in never broken. To fix this try:


while True:
---userInput = input(">>> ")
---if userInput in ['hi', 'HI', 'Hi']:
------print("Hello")

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

that's the idea...

About This Instructable

58,294views

18favorites

License:

Bio: 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 ... More »
More by BlueBlood Studios:Teach Me Python #7: Control Statements Pt.2: While LoopsTeach My Python #6: Control Statements Pt.1: If, Else And ElifTeach Me Python #5: Simple Maths
Add instructable to: