MadLibs help you to create funny little tales through its interactive templates. MadLibs come in all sorts of platforms (e.g., newspapers, puzzle books, online games), but you can take it up a notch by creating your own.
loop = 1 while (loop < 10): #All the questions that the program asks the user pnoun1 = "monkeys" adj1 = "cute" pnoun2 = "dolphins" pnoun3 = "snowglobes" adj2 = "weird" color = "color! pink" adj3 = "bald" noun1 = "spoon" pnoun4 = "cats" adj4 = "sweet" place = "New york" #Displays the story based on the users input print ("\n-------------Here's Your very own Unicorn MadLib Story!-----------------------------") print ("Unicorns aren't like other",pnoun1,";they're ", adj1,".\nThey look like ",pnoun2,",with ",pnoun3,"for feet and a",adj2,"mane of hair.") print ("But unicorns are",color,"and have a",adj3,noun1,"on their heads.") print ("Some",pnoun4,"don't believe unicorns are",adj4,",but I believe in them!") print ("I would love to take my unicorn to",place) #Loop back to "loop = 1" loop = loop + 1
Multiple string variables are declared in order to store user details.
Once all the blanks are filled by the player, the control is then passed to the printing of the player’s story to read!
Prior to the declaration in the code, a looping variable called loop, which is initialized to
1, is declared.
A while loop is used with loop as its looping variable under which all the
print() statements are written.
The condition for the number of iterations under while refers to the number of times the player will be able to play around and create stories.
This means that, initially,
loop=1 will be incremented each time a story is printed. While this loop value stays within the condition of the while loop, the code under it will be executed that many times. Here, we have given
10 as the looping variable, so the player can keep filling in the blanks to create up to 9 stories!
However, you can change this static loop value to make it dynamic and enhance the game’s code. This loop’s value can be taken from the player themselves through an
input() statement that will hold the number of times they want to play the game.
The code used for this would be:
loop=int(input("How many times do you want to play?"))
View all Courses