Random Joke Generator in Python.
About the project: This program will simply pick a joke from its collection and display it to you.
You can keep asking for new jokes until you decide to quit.
How to use this program:
Run the Python script.
When you run it, you can simply press Enter to get a new random joke.
and it will continue to provide them until you type q to quit.
you can add more jokes as many you like within code.
Project Level: Beginner
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# random_joke_generator.py
import random
def get_random_joke():
"""
Returns a random joke from a predefined list.
You can easily add more jokes to this list!
"""
jokes = [
"Why don't scientists trust atoms? Because they make up everything!",
"What do you call a fish with no eyes? Fsh!",
"Why did the scarecrow win an award? Because he was outstanding in his field!",
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
"What do you call a fake noodle? An impasta!",
"Why did the bicycle fall over? Because it was two tired!",
"What's orange and sounds like a parrot? A carrot!",
"I'm reading a book about anti-gravity. It's impossible to put down!",
"What do you call a sad strawberry? A blueberry!",
"Why did the math book look sad? Because it had too many problems."
]
return random.choice(jokes)
def main():
"""
Main function to run the Random Joke Generator in a continuous loop.
"""
print("--- Python Random Joke Generator ---")
print("Press Enter to get a new joke, or type 'q' to quit.")
while True:
user_input = input("\nReady for a joke? (Press Enter or type 'q'): ").strip().lower()
if user_input == 'q':
print("Exiting Joke Generator. Hope you had a laugh! Goodbye!")
break
elif user_input == '':
joke = get_random_joke()
print("\n--- Here's your joke! ---")
print(joke)
print("--------------------------")
else:
print("Invalid input. Press Enter for a joke, or 'q' to quit.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()