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.
Overview
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.
Instructions
- Open the Excel sheet and access the three tabs containing the list of nouns, verbs, and adjectives
- Read in the tabs and create three lists – a noun list, an adjective list, and a verb list – to store the data
- 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
Hint
#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]
- 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’]:
break
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
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
break
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
mad_libs()
Challenge88_Solution.txt
Solution Video: Challenge 88|PYTHON – Mad Libs Help