Python-based project for a Mad Libs game
Here’s a Python-based project for a Mad Libs game. This project will prompt users to input specific types of words (e.g., nouns, verbs, adjectives) and then use those inputs to generate a fun and creative story.
Features of the Project
- Interactive user input to gather words.
- Predefined story templates with placeholders for the words.
- Dynamic generation of the story using user inputs.
- Option to play again with a different story.
Code Implementation
import random
def get_user_input(word_type, prompt=None):
"""
Function to get user input for a specific word type.
"""
if not prompt:
prompt = f"Enter a {word_type}: "
return input(prompt).strip()
def generate_story(template, user_words):
"""
Replaces placeholders in the story template with user inputs.
"""
try:
return template.format(*user_words)
except IndexError:
print("Error: Not enough inputs to fill the template!")
return ""
def play_mad_libs():
# Define a list of Mad Lib templates
templates = [
"Once upon a time, in a {0} {1}, there was a {2} {3} who loved to {4}.",
"Yesterday, I saw a {0} {1} {2} around the park. It was so {3}!",
"In a {0} {1}, a group of {2} decided to {3} a giant {4}.",
"The {0} {1} jumped over the {2} {3} while shouting, '{4}!'",
"I went to the zoo and saw a {0} {1} eating a {2}. It was so {3} that everyone {4}."
]
# Randomly choose a template
template = random.choice(templates)
# Determine placeholders to replace
num_placeholders = template.count("{")
word_types = ["adjective", "noun", "verb", "place", "exclamation"] # Generic types
print("\n** Welcome to Mad Libs! **")
print("Please provide the following words:")
user_words = []
for i in range(num_placeholders):
word = get_user_input(word_types[i % len(word_types)])
user_words.append(word)
# Generate and display the story
story = generate_story(template, user_words)
print("\n** Here's your Mad Lib! **")
print(story)
# Option to play again
replay = input("\nWould you like to play again? (yes/no): ").strip().lower()
if replay == "yes":
play_mad_libs()
else:
print("\nThank you for playing Mad Libs!")
if __name__ == "__main__":
play_mad_libs()
How It Works
-
Random Story Selection:
- The program has a list of story templates with placeholders.
- One template is randomly selected for each round.
-
User Inputs:
- Users are prompted to enter specific types of words (e.g., adjective, noun, verb).
- The input words are collected and stored.
-
Story Generation:
- The placeholders (
{}
) in the story are replaced with the user inputs using Python’sformat()
function.
- The placeholders (
-
Replay Option:
- After the story is displayed, the user can choose to play again with another random template.
Sample Run
Input:
** Welcome to Mad Libs! **
Please provide the following words:
Enter a adjective: mysterious
Enter a noun: forest
Enter a verb: explore
Enter a place: castle
Enter a exclamation: Eureka!
** Here's your Mad Lib! **
Once upon a time, in a mysterious forest, there was a mysterious castle who loved to explore.
Would you like to play again? (yes/no): no
Output:
Thank you for playing Mad Libs!
Enhancements to Consider
-
Dynamic Word Types:
- Make the word types depend on the chosen template, not hardcoded.
-
GUI Version:
- Use a library like Tkinter to create a graphical version of the game.
-
Save Stories:
- Allow users to save their generated stories in a file for later reading.
-
More Templates:
- Expand the list of story templates for more variety.
-
Randomized Inputs:
- Allow a random mode where inputs are auto-generated for quick play.
Let me know if you'd like me to expand the project further or add enhancements!
Comments
Post a Comment
"Thank you for seeking advice on your career journey! Our team is dedicated to providing personalized guidance on education and success. Please share your specific questions or concerns, and we'll assist you in navigating the path to a fulfilling and successful career."