240 likes | 254 Views
Learn how to create your own artificially intelligent chatbot program in Python. Understand the basics of AI and how it can be used to develop chatbot applications.
E N D
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program
http://www.titane.ca/main.html • Click to chat! Ask God anything you like! Spend a few minutes Checking out this program. Obviously – it isn’t really God you’re speaking to (although you never know!) In this lesson we’ll be making our own artificially intelligent Chat bot!
What is AI? • Artificial Intelligence (AI) technology provides techniques for developing computer programs that carry out a variety of tasks, simulating the intelligent way of problem-solving by humans. The problems that humans solve in their day-to-day lives are of a wide variety and in different domains. Although the domains are different and so are the methods, And what about a CHATBOT? • A chatbotis basically a chatting robot that can understand what you are saying, analyse it and give you a suitable response. It's considered to be a serious branch in AI development, as the purpose of programming a chatbot is to help people obtain information. Examples include: • Selling chatbots: These help people to know item prices and offers. • Supporting chatbots: You may find this kind of chatbot on websites that offer products and services. • Help desk (information desk) chatbots: You may find these in large libraries, websites or programs. • Entertaining chatbots: These are made for fun and chatting.
Video from Stanford about the future of Robotics and Artificial Intelligence … http://www.youtube.com/watch?v=AY4ajbu_G3k
Next - • Let’s look at how to do a few basic things in Python • SAVING A PROGRAM • OPENING A SAVED PROGRAM • Functions in Python (the PRINT and INPUT Functions) • CREATING A CHAT BOT (appearing to be artificially intelligent)
Saving a Python Program File – Save As – NameOfProgram.py
Opening a saved Python Program File – Open // Run - Run Module
Opening • Opening The Programs You've Saved • To load a saved program, choose File > Open. Do that now, and in the window that appears choose hello.py and press the Open button. Your saved hello.py program should open in the File Editor window. • Now it's time to run our program. From the File menu, choose Run > Run Module or just press the F5 key on your keyboard. Your program should run in the shell window that appeared when you first started IDLE.
Functions in Python The Print Function The Input Function
Functions Think of afunctionas a sort of mini-program inside your main program. Python has a number of built-in functions such as the prnit() function that we have just looked at. A function call is a piece of code that tells our program to run the code inside a function. For example, your program can call the print() function whenever you want to display a string on the screen. The print() function takes the string you type in between the parentheses as input and displays the text on the screen.
The print() Function print(‘I Love TeachingComputing’) print(‘Whazzap?’) This line is a call to the print function, usually written as print() The string (of characters or text or whatever) to be printed goes inside the parentheses. Note: The parentheses at the end of the function inform us that we are referring to a function, much like the quotes around the number '47' tell us that we are talking about the string '42' and not the integer 47.
The input() Function The input() Function myFavNumber= input() This line has an assignment statement with a variable (myFavNumber) and a function call (input()). When input() is called, the program waits for input; for the user to enter something. The text string that the user enters (your favourite number) becomes the function's output value.
Try it yourself Creating a Simple Chatbot 1 Open the Python Shell IDLE GUI. 2 Go to File – New 3 Type in the following code (the first line is a comment)
4 Go to Run – Run Module (or just press F5) 5 It will prompt you to save – save as something and end it with “.py”
6 When the program runs – it will look something like this (see below) print ('Hello there, do you have a favourite number?') favnumber=input() print (favnumber + ' is a silly number. 5.22269627 is better I think') print ('bye for now ...') Code to cut and paste
BINGO Game • The “BINGO" Game • We are going to make a little BINGO interactive game. You will program the computer to think of a random number from 1 to 20, and ask the user to guess the number. The user will only get six guesses, but the computer will tell the user if his/her guess is too high or too low. If the user guessesthenumber within six tries, he/she wins – BINGO! • This game gets us using random numbers, loops, and input from the user so lots of good practice. You will also learn how to convert values to different data types
Sample Run of “BINGO" Here is what our game will look like to the player when the program is run. The text that the player types in is in bold. Hello! What is your name?MooseWell, Moose, see if you can guess the number between 1 and 20.If you win, I’ll say BINGO.10Er, too high.Take a guess.2Nope, too low.Take a guess.4BINNNNGGGGGO, Moose! You guessed the number in 3 guesses!
Try it yourself Code to cut and paste # This is a BINGO Game import random guessesTaken = 0 print('Hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', see if you can guess the number between 1 and 20.If you win, I’ll say BINGO.') while guessesTaken < 6: print('Take a guess.') # There are four spaces in front of print. guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 if guess < number: print('nope too low...') # There are eight spaces in front of print. if guess > number: print('that is too high.') if guess == number: break if guess == number: guessesTaken = str(guessesTaken) print('BINNNNGOOO, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!') if guess != number: number = str(number) print('Nope. The number I was thinking of was ' + number) 1 Open the Python Shell Go to File – New Window Type in or paste the code shown File – Save as – Bingo.py Press F5 to Run the Program! 2 3 4 5
CODE OUTPUT
A chance to reflect on the future of Computer Science – Careers – Opportunities and How to succeed What do you think Computer Science is? http://www.youtube.com/watch?v=qFH9gVvHqWg