Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.

Similar presentations


Presentation on theme: "Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program."— Presentation transcript:

1 Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program

2 Creating a ChatBot

3 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!

4 Artificial Intelligence

5 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 chatbot is 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.

6 Video from Stanford about the future of Robotics and Artificial Intelligence …

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

8 Saving a Python Program
File – Save As – NameOfProgram.py

9 Saving Once you've entered your source code, save it so that you won't have to retype it each time you start IDLE. To do so, choose the File menu at the top of the File Editor window, and then click on Save As. The Save As window should open. Enter hello.py (or whatever you want to call it as long as it ends in “.py” in the File Name box then press Save. You can use the shortcut CTRL+S to save quickly!

10 Opening a saved Python Program
File – Open // Run - Run Module

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

12 Functions in Python The Print Function The Input Function

13 Functions Think of a function as 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.

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

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

16 Creating a Simple Chatbot
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)

17 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”

18 When the program runs – it will look something like this (see below)
6 When the program runs – it will look something like this (see below) Code to cut and paste print ('Hello there, do you have a favourite number?') favnumber=input() print (favnumber + ' is a silly number is better I think') print ('bye for now ...')

19 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 guessesthe number 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

20 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? Moose Well, Moose, see if you can guess the number between 1 and 20. If you win, I’ll say BINGO. 10 Er, too high. Take a guess. 2 Nope, too low. Take a guess. 4 BINNNNGGGGGO, Moose! You guessed the number in 3 guesses!

21 Type in or paste the code shown
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 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

22 CODE OUTPUT

23 What do you think Computer Science is?
A chance to reflect on the future of Computer Science – Careers – Opportunities and How to succeed

24 End of Lesson 4


Download ppt "Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program."

Similar presentations


Ads by Google