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
Nancy Melucci
Courses Plus Student 36,282 PointsPython Homework Help
I am taking a Python class and I am stuck on my homework problem. I am not asking anyone to write the code for me, I just need a hint where to find a function or method that will help me finish. What I have so far is:
A string that represents a form letter with placeholders. Three tuples that contain information to be inserted into the placeholders. A list with the three tuples as list items.
What I've done so far is: Make the tuple members appear correctly in a single attempt (so I know how to put them in to the form letter.)
What I need to do is: Find a method that will take each member of the list of tuples (that is, each tuple) appear in one version of the letter. Then stop....having produced three letters.
I am learning currently: HTML/CSS and other web development languages, Java, and Python. I enjoy Python but have the most trouble with it. I'd like to pass the class for which this is an assignment, and finish Kenneth L's tutorials here. So, if someone could help me get unstuck with a suggestion about what method will help me finish this and (I hope) learn something more about the functionality of Python, I'll be grateful.
Thanks again
8 Answers
Joseph Kato
35,340 PointsIf I'm understanding the problem correctly, I don't think you're going to find a built-in function which does exactly what you need.
I think your best option is to write your own function, using the code which produces one correctly formatted letter inside of a for-loop. In which case, the length of the tuple-containing list represents the number of iterations necessary to produce the correct number of letters.
(I'd elaborate on what I mean, but I'm not sure how much outside help you're allowed.)
Sarfaraj Alam
14,074 PointsI am good at Python Programming assignment help. But most of the time, I am busy with my personal python project. but you are looking for some who provide Python homework help
I hope you like their support and service.
Sarfaraj Alam
14,074 PointsI also find a very interesting service
Nancy Melucci
Courses Plus Student 36,282 PointsThank you - that makes sense. I hope I can figure out how to make that loop.
Appreciate your taking the time.
I don't want to run afoul of the academic integrity rules....I may post my code and see what you think. I'll check back in later.
Nancy M.
Nancy Melucci
Courses Plus Student 36,282 PointsI think I cracked it - I am supposed to use lists and tuples to make the form letter...this is what I came up with. It runs....I just have to make it neater and then I can submit it to the teacher for grading...
Thanks for your help!
Create a spambot type letter exhorting the lucky recipient to vote for a particular candidate.
My voter tuples
voterInfo1 = ('Fred', 'Joe Rockhead', 'Barney' ) voterInfo2 = ('Mr. Gumby', 'Dinsdale Pirahna', 'Anne Elk' ) voterInfo3 = ('Gus', 'Det. Lassiter', 'Sean' )
my list of tuples
List = [('Gus', 'Det. Lassiter', 'Sean' ),('Mr. Gumby', 'Dinsdale Pirahna', 'Anne Elk' ),('Fred', 'Joe Rockhead', 'Barney' ) ]
the code to make three form letters
print ( 'Dear %s, \n \n I would like you to vote for %s. \n \n This is the best candidate for the state.\n \n Yours in Politics \n\n %s\n' % List[0] ) print ( 'Dear %s, \n \n I would like you to vote for %s. \n \n This is the best candidate for the state.\n \n Yours in Politics \n\n %s\n' % List[1] ) print ( 'Dear %s, \n \n I would like you to vote for %s. \n \n This is the best candidate for the state.\n \n Yours in Politics \n\n %s\n' % List[2] )
Joseph Kato
35,340 PointsNicely done!
The only thing I noticed is that you're not using a function (outside of print, anyway), is that okay?
Nancy Melucci
Courses Plus Student 36,282 PointsI haven't handed it in for grading....if there is a way to design a function that will make it a cleaner, more efficient piece of coding, I am open to your guidance. Right now I was just aiming to do what the instructor asked...to use lists and tuples to make the form letter happen. And I wanted to write my own code, because I don't want to cheat. If you can point me in the direction of a function to insert the list members into the letter at the right spot, I am interested. Thanks for your encouragement. I'll probably hand it in early Saturday (via internet. - It's due Monday but I hate to cut it close...she also gives 3 tries, and like many programming instructors, is very particular about how these things are done. If I get it in early, I get another shot if she is not pleased with it.)
Nancy M.
Joseph Kato
35,340 PointsThat's a nice grading policy; I'd definitely take advantage of the extra tries.
In terms of cleaner and more efficient code, the only area for improvement I see is your usage and format of the placeholder string. I would format the string so that it resembles its final state a little more; for example:
template = (
"Dear %s,\n"
"\n I would like you to vote for %s.\n"
"\n This is the best candidate for the state.\n"
"\n Yours in Politics,\n"
"\n%s\n"
)
Then, instead of re-writing the string, I'd use a loop inside a function, such as:
def fill_template(template, content):
"""Print different versions of template, as specified by content.
Parameters:
template (str) -- A pre-formatted string with placeholders.
content (list) -- A list of tuples containing information
to be added to template.
"""
for version in content:
#Your code here
#version represents an element of content,
#so you only need to write one print statement.
That said, since this is just my opinion, I'd focus on doing exactly what your teacher is looking for.
Good luck!
Nancy Melucci
Courses Plus Student 36,282 PointsThat rocks. Thanks so much....I am learning more from my registered college classes because I have this resource available to me. Have a good evening. Nancy M.
Nancy Melucci
Courses Plus Student 36,282 PointsHere's the final version. I'm very proud of it. Thanks again...
This is a fun for-loop using lists and tuples to create a political spambot form letter with three versions
List of victims...er...voters. List members are tuples
List = [ ('Gus', 'Det. Lassiter', 'Sean' ),('Mr. Gumby', 'Dinsdale Pirahna', 'Anne Elk' ),('Fred', 'Joe Rockhead', 'Barney' ) ]
for loop with template for letter
for i in range(len(List)): print ( 'Dear %s, \n \n I would like you to vote for %s. \n \n This is the best candidate for the state.\n \n Yours in Politics \n\n %s\n' % List[i] )
My references
How to think like a computer scientist http://interactivepython.org/runestone/static/thinkcspy/toc.html
Dive Into Python http://www.diveintopython.net/file_handling/for_loops.html
Picked the brains of my friends at Treehouse http://www.teamtreehouse.com
Joseph Kato
35,340 PointsNice, it looks great!
I have a minor suggestion, though: you don't have to use indices to access the elements of List. In general, it's considered more "Pythonic" to loop like this:
for names in List:
print('...your string...' % names)
Nancy Melucci
Courses Plus Student 36,282 PointsThanks...we have three tries. If she zeroes me out and I end up trying again, I'll make this modification along with whatever the instructor wants changed. Have a good evening!