Python 1.1.0 Help

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:

  1. You can use the editor built into this webpage

  2. You can use Visual Studio Code for the web

  3. You can use Replit

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

userResponse = input("What's your favorite Color? ") print(f"Hey, mine's {userResponse} too!")

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

jokeList = [ "Is it true that cannibals don't eat clowns because they taste funny?", "What kind of pig can you ignore at a party? A wild bore.", "What kind of murderer has fiber? A cereal killer." ]

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.

for jokes in range(len(jokeList)):

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.

if jokes == 0: userAnswer = input("Would you like to hear a joke?: ") else: userAnswer = input("Would you like to hear another?: ")

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

if "y" in userAnswer: print(jokeList[jokes]) elif "n" in userAnswer: break else: print("I don't know that one yet try again\n") continue

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.

print("Well, that's all folks 👋")

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)

# List initiation jokeList = [ "Is it true that cannibals don't eat clowns because they taste funny?", "What kind of pig can you ignore at a party? A wild bore.", "What kind of murderer has fiber? A cereal killer." ] # Loop named 'jokes' that will loop for the length of the jokeList for jokes in range(len(jokeList)): # Check to see if this is the first runthrough if jokes == 0: userAnswer = input("Would you like to hear a joke?: ") # Fallback for every other execution else: userAnswer = input("Would you like to hear another?: ") # Check to see if the user's answer contained a 'y' if "y" in userAnswer: # Print the joke corresponds to the loop itteration we are on print(jokeList[jokes]) # Check to see if the user's answer contained a 'n' elif "n" in userAnswer: # Exit the loop and run code below it break # Fallback in-case it didn't contain either else: # Inform the user that their input was not recongnized print("I don't know that one yet try again\n") # Restart the loop continue # A farewell (ie. Code that runs after the loop) print("Well, that's all folks 👋") # Written by Jake Rase on May 24th, 2024
Last modified: 25 May 2024