Project One | Jokebot
Intro
Welcome to your first project, this project will be interactive and allow to follow the instructions to write your first code. You have a few options for actually writing code for this project:
You can use the editor built into this webpage
You can use Visual Studio Code for the web
You can use Replit
Or you can download a desktop code editor (I would personally recommend one of two options, Visual Studio Code from Microsoft, or Pycharm from Jetbrains)
Built in Editor
Directions
This project's objective is to build a simple program to tells jokes and puns, its really pretty simple, we ask the user if they would like to hear a joke, if they say yes, then we tell a joke, if they say the program exits. For this project, you will need to use Python's input()
function, this function takes one argument, and poses the argument as the prompt for the user, the user's response then becomes the value returned from the function.
Example:
This script asks the user for their favorite color, then their answer is given to the variable userResponse
and printed within an f-string.
Step One
First off, we need some jokes, you can go here and pick a few. Once you have a few we are going to create a list of these jokes, I'm only going to use three, but you can use as many as you'd like
Step Two
Ok, now we need to be able to tell each of these jokes, while we could hard code an if statement to print each statement individually, it's much more efficient and readable to use a loop, in this case we are going to call this loop "jokes", it is going to run the code inside the same number of times as there are items in the list, that way we can tell each joke regardless of how many jokes there are.
Step Three
Now we need to ask the user if they would like to hear our wonderful jokes, we are going to use an if statement to check and see if this is the first time the loop has run, because we don't want to ask the user the same question every time. The reason this works is because, in a loop, the name of the loop is given the value of the number of times the loop has executed its chunk of code, in our case, the loops name is jokes
, and all loops start at 0
, so by checking if jokes == 0
, we are checking to see if this is the first execution. The else statement is used so that every other time the user is prompted to continue, it uses the second question instead.
Step Four
Now we finally get to the good part, telling the jokes. After the user has answered, we check to see if the user has said yes or no, if they have say yes (or any anything containing a 'y') we tell a joke and ask again, if they say no (or any anything containing a 'n'), we use the break
keyword to tell Python to exit the loop and execute any code after the loop, if the user says anything that doesn't fall under those two conditions, we tell the user that their input was unrecognized (with the print statement) and restart the loop (using the contiunue
keyword).
Also, as an important sidenote, if you have a keen eye, you may have noticed a \n
at the end of one of the print statements, in Python the backslash is known as an 'escape character'. This means that whatever character follows it has a different effect, in this case, our following character is an 'n', this creates a new line in the terminal, in this case there is nothing after the \n
statement so the new line is empty, however anything placed after the \n
statement wil be printed in the next line
Step Five
Finally, we have arrived at the end of our little adventure, all we have to do now is say goodbye, so right below our loop we will bid our farewell.
Full Script
This represents a template for you to compare your script to, yours doesn't need to look exactly like mine, (That's one of the great things about programming) but if you have trouble, there is of course no shame in copying and pasting. (Half of my day is simply ⌘C + ⌘V)