Back to Home
Programming a chat bot was the first task assigned to us. In order to make a chat bot I knew there had to be a lot of user input, and in order to do that I had to use a lot of "raw_input"'s in my code. Here is an example of a function in which the user inputs their name and how they are feeling.
You can also make the program target specific words an give responses that the coder has coded in. What you have to do is use these if and else-if statements that make it so if a specific word is in the user's response, the program will give a predetermined response. Here is an example of them:
The last thing that I programmed into the chat bot was a yes or no function. This is something that I will continue to use in many of my python programs that use user input. What this does is ask the user a question and depending on if they respond with 'y' or 'n' the program can do a predetermined action. This can be used to ask if a user wants to ask a question, or if a user would like to have the first move in a game.
The next thing that I did was make a Fact bot. What the program did was ask the user for their question and give them back an answer if possible. This was accomplished by using something called Wolfram Alpha. My program could access the Wolfram Alpha database using an app i.d. that I got by signing up for an account on their website.
Using this code I was able to get the answers to most questions. If Wolfram Alpha was not able to answer the question, then I made my python program able to say that it didn't have an answer to the question. Here are some questions that the program did answer successfully though:
As you can see this is a pretty interesting program.
Making the rock paper scissors program was not too hard to do. What the program would do was have the user input a number 1-3, each representing either rock, paper, or scissors. The computer would then pick a random number and then compare the two numbers to determine if the user won, lost, of if it was a tie.
This was based off of James Bamford's compact Rock, Paper, Scissors design.
Here is a picture of the code in action:
I did not do very goo d but you can still see that the code works!
Even a simple game such as tic tac toe was really hard to make in python. The first thing I did was make a 2 player game.
The intro part was a simply intro to the game. The drawboard() function however is far more important. It is the game board for the game that will continually change as the game goes on and as the players choose spaces.
This function asks the player for their move. It also checks to see if that space on the board is taken or if whatever the player typed is not a value between 1 and 9.
While this function is not very lengthy, what it does is it applies the move that the player just inputted to the board that I mentioned in step 1.
These two functions check if the game will be either a tie or a win for one of the players. In order to check if the game is a win for one of the players I had to create this list with all of the possible ways for a player to win and the computer would check those spaces and see if they were in the same state of 'X' or 'O'. The check tie function just checked to see if there were no blank spaces on the board after the check win function turned up false.
All this function does is ask the user if they would like to play again, short and simple as that.
What this does is put together all of the functions in the correct order and make the game of tic tac toe work.
To adapt the two player to a single player game in which you play against a computer you need to replace the get move function for player two to a new one in which the computer ideally chooses intelligently and makes it impossible for the human player to win. Unfortunately I could never actually get this to work despite trying for many hours :(