88|PYTHON – Mad Libs Help

BYU Student Author: @IWillyerd
Reviewers: @Erick_Sizilio, @Jae
Estimated Time to Solve: 20 Minutes

We provide the solution to this challenge using:

  • Python

You can use Google Collab as the Integrated Development Environment (IDE) to complete the challenge. The challenge solution uses both the Pandas library and “Random” library.

Need a program? Click here.

You have just started dating someone who loves to play Mad Libs, a word game where you input a noun, adjective, or verb to change the story at certain moments. However, you struggle to come up with appropriate words to use in the game. Fortunately, you stumbled upon an Excel sheet containing three tabs filled with adjectives, verbs, and nouns that are perfect for Mad Libs. As a result, you decide to utilize your Python skills to create a function that will randomly generate a word and aid you in improving your Mad Libs abilities.


  1. Open the Excel sheet and access the three tabs containing the list of nouns, verbs, and adjectives
  2. Read in the tabs and create three lists – a noun list, an adjective list, and a verb list – to store the data
  3. When reading in the lists clean the inputs as necessary, make sure to account for any white space in each list item and remove it before or after storing the item in the list
    #Read in the data from the excel as a Panda’s DataFrame then use the following code to convert to a list  
    my_list = df[‘Words’].tolist() 
    #One line code to clean each of the list items 
    my_list = [string.strip() for string in my_list] 
  4. Create a function that will return (or print) a random verb, noun, or adjective from the lists you created based on what the user specifies. Additionally, ensure the function does the following:
    • The user needs to specify which list they want a word from. Additionally, embed the input into a loop so that they will be prompted to keep trying until they type in an accepted input.
    • Your code should eliminate any white spaces so that the function works no matter how much white space is typed by the user.
    • The responses from the user should also be case insensitive.
Suggestions and Hints
#Use a while True loop to get the correct input try the following format 
    while True: 
        my_input = my_input(‘Input a number 1-3’) 
        if input in [‘1’,’2’,’3’]: 
        print('Input a number 1-3’)  

#Use the following code to make it insensitive or to deal with spaces 
my_input = my_input.strip() #Gets rid of whitespace before and after 
my_input = my_input.upper() #Makes the string all uppercase to make it case insensitive (can also use .lower()) 

Data Files


Solution Code
#Reads in Packages 
import pandas as pd 
import random 

#Reads in all the sheets as a df to create a list 
df_noun = pd.read_excel('Challenge88_Word_Lists.xlsx',sheet_name='Nouns') 
df_adj = pd.read_excel('Challenge88_Word_Lists.xlsx',sheet_name='Adjectives') 
df_verb = pd.read_excel('Challenge88_Word_Lists.xlsx',sheet_name='Verbs') 

#Converts all the dataframes into a list 
noun_list = df_noun['Words'].tolist() 
adj_list = df_adj['Words'].tolist() 
verb_list = df_verb['Words'].tolist() 

#Strips all the whitespace from each of the strings 
noun_list = [string.strip() for string in noun_list] 
adj_list = [string.strip() for string in adj_list] 
verb_list = [string.strip() for string in verb_list] 

#Creates the function that you'll call to get a random word from the list 
def mad_libs(): 
    #Creates a loop that won't break unless the user inputs the necessary word 
    while True: 
        choice = input('Select either "Noun","Adjective",or "Verb"')#Takes in user input 
        choice = choice.strip()#strips out any spaces if that was input 
        choice = choice.upper()#makes it all uppercase so that its case INSENSITIVE 
        if choice in ['NOUN','ADJECTIVE','VERB']:#compares the choice to the list of okay words that will break the loop if correct 
        print('Please input "Noun","Adjective",or "Verb"')#string that will print everytime they input the wrong response 

    #IF statements that will correctly return the word from the selected list 
    if choice == 'NOUN': 
        word = random.choice(noun_list)#random function will randomly select a word from the list 
    if choice == 'ADJECTIVE': 
        word = random.choice(adj_list) 
    if choice == 'VERB': 
        word = random.choice(verb_list) 

    print(word)#prints the random word 

#Calls the random word function 

Solution Video: Challenge 88|PYTHON – Mad Libs Help

Fun challenge! I followed your hints and went down a very similar path in my code. I only had slight variations that come from my personal style. I had a little trouble at the end using a Userinput as my variable rather than choice. Your solution helped me understand the issue and clear it up!

Here is my solution! I appreciated your notes you put in your solution code, it helped me understand how to use the ‘random’ method/function. Thanks!

Fun challenge! Good reminder of loop functions and the random function. My code is pretty similar to the solution minus some style things. I did use the hint to get the tolist() part which was super helpful! My images won’t attach but this is the code I used for the def function part:

list4 = [‘NOUN’,‘ADJECTIVE’,‘VERB’]
def words():
while True:
category = input(‘Type in “Noun”,“Adjective”,or “Verb”’)
category = category.strip()
category = category.upper()
if category in list4:
print(‘Your input is incorrect. Please try again. Type in “Noun”,“Adjective”,or “Verb”’)
if category == ‘NOUN’:
word = rd.choice(list)
if category == ‘ADJECTIVE’:
word = rd.choice(list2)
if category == ‘VERB’:
word = rd.choice(list3)