Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sharon Walls
9,234 PointsWhen to use variables and when to use lists?
I'm working on a simple Mad Libs Python script and I'm a bit stuck on whether to use variables or lists for the blank words. To my understanding, variables and lists both hold values I can call later, so I'm struggling with which is best to use (and why).
What is the most Pythonic way to do it?
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsIn this case, Pythonic may be moot. Both individual variables and a list of user responses both work equally as well. What might not be Pythonic is the way in which either approach is implemented. In terms of learning, try both and see which seems more fitting. I hesitate to post specific suggestions so that you will get the most learning experience from your project. I'd be curious to see your code. Please it to the forum if you wish.
Sharon Walls
9,234 PointsThank you both for your answers. This is what I have so far. It works, but I think it could be simplified:
print("Welcome to Mad Libs - Python Edition. Follow the prompts to create your own hilarious story.")
proper_name1 = raw_input("Enter a name ")
verb1 = raw_input("Enter a present-tense verb ")
proper_name2 = raw_input("Enter a name ")
noun1 = raw_input("Enter a proper title ")
place1 = raw_input("Enter a place ")
num1 = raw_input("Enter a number ")
noun2 = raw_input("Enter a plural noun ")
place2 = raw_input("Enter a place ")
noun3 = raw_input("Enter a noun ")
num2 = raw_input("Enter a number ")
noun4 = raw_input("Enter a plural noun ")
percent1 = raw_input("Enter a number ")
print("Dear {}, It is my pleasure to {} to you today. I am {} and I'm the {} of {}. I have inherited {} {} but I need your help to get it to {}. Please send me your {} and the sum of {} {} to get started, and I will give you {} percent of my inheritance. Yours truly, {}".format(proper_name1, verb1, proper_name2, noun1, place1, num1, noun2, place2, noun3, num2, noun4, percent1, proper_name2))
Chris Freeman
Treehouse Moderator 68,468 PointsSimple and direct works. It seems you're using Python 2 based on the raw_input() functions. For Python 3 this will change to input(). Using print as a function, the parens allow you to break the text into separate lines:
print("Dear {}, It is my pleasure to {} to you today. I am {} and I'm the "
"{} of {}. I have inherited {} {} but I need your help to get it to {}. "
"Please send me your {} and the sum of {} {} to get started, and I will "
"give you {} percent of my inheritance. Yours truly, {}".format(
proper_name1, verb1, proper_name2, noun1, place1, num1, noun2,
place2, noun3, num2, noun4, percent1, proper_name2))
Conceivably, a Mad Lib constructor could be built that parsed a string or paragraph extracting marked words to ask the user to provide then combine them to produce the output. That would be a much more intense project. Keep it in mind as you learn more Python. It might fun to add a constructor feature.
Sharon Walls
9,234 PointsThanks for your feedback, Chris. I appreciate it!
Matthew Smith
2,668 PointsHello Sharon,
I saw that your question has been answered but I did this last night and though you would like it.
'''python import os import sys
def start(): print("Welcome to Mad Libs - Python Edition. Follow the prompts to create your own hilarious story.") userinput1 = input("If you would like to exit, type 'Q'. Otherwise, press enter") if userinput1.lower() == 'q': sys.exit() else: clear() game()
def clear(): if os.name == 'nt': os.system('cls') else: os.system('clear')
def game(): proper_name1 = input("Enter a name ") verb1 = input("Enter a present-tense verb ") proper_name2 = input("Enter a name ") noun1 = input("Enter a proper title ") place1 = input("Enter a place ") num1 = input("Enter a number ") noun2 = input("Enter a plural noun ") place2 = input("Enter a place ") noun3 = input("Enter a noun ") num2 = input("Enter a number ") noun4 = input("Enter a plural noun ") percent1 = input("Enter a number ")
clear()
print("""Dear {},
It is my pleasure to {} to you today. I am {} and I'm the {} of {}. I have inherited {} {} but I need your help to get it to {}. Please send me your {} and the sum of {} {} to get started, and I will give you {} percent of my inheritance. Yours truly, {}""".format(proper_name1, verb1, proper_name2, noun1, place1, num1, noun2, place2, noun3, num2, noun4, percent1, proper_name2) )
start() '''
My best regards, Matt
Sharon Walls
9,234 PointsLooks great! Very clean.
I've just finished some of the game challenges so I definitely plan on improving it like you did.
Matthew Smith
2,668 PointsMatthew Smith
2,668 PointsHey Sharon,
I'm not 100% on this as I have just begun learning as well but I think the most concise way to do this would be to use a list to store user input because it eliminates the potentially large amount of variables that you would have to reference individually. Instead you could just reference the user input by its index in the list.
Let me know if this helps, Matt