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 trialPhilip Holtzman
6,318 PointsBasic Python course doesnt prepare student enough for LOGIC needed for Collection Exercises, Logic tutorial elsewhere???
After breezing through the Basic Python course(solving almost every problem without help), I find myself completely unable to solve the logic problems of the Python Collection Exercises. I have gone through all the activities and completed all but the final one(using help from the forums for each previous one.)
I find there is a bit of a quantum leap in the logic needed in the Python Collections. Is there another resource anyone who has mastered this could recommend me? perhaps a tutorial that goes slower or explains more logic? I am fine with the data types and syntax. I can understand the logic AFTER I read someone elses clear and concise code, but jsut producing my own logical solutions for the python collection questions out of thin air I find overwhelming.
8 Answers
Josh Keenan
20,315 PointsTo be honest I'm not sure, I didn't get it before but I got it too! I know they have been updating and tinkering with python stuff on treehouse a lot recently and there was an issue of Python 2 being used instead of Python 3.
Since writing this solution however I have created a slightly different one, done in fewer lines of code.
def string_factory(dicts, string):
list1 = []
for dictionary in dicts:
list1.append(string.format(name=dictionary['name'], food=dictionary['food']))
return list1
I will have a play with that older solution as I test all my solutions before posting to ensure I don't give false information so I know it did work! However this is my newer iteration on the solution. If you want me to explain it just ask :)
Josh Keenan
20,315 PointsI advise that you watch the previous video for whatever challenge you get stuck on, if you understand the part of the challenge that is related to the previous topic but don't understand functions or loops or something that you use in the challenge that isn't related to your topic directly, see if there's a course or workshop on it (Go to Kenneth's page on treehouse https://teamtreehouse.com/kennethlove). If not ask on the forums and we will do our best to explain things for you, there's always the option to look up things on the internet, don't always trust what you see on stack overflow it's not always right. But I believe Kenneth is re-vamping the python basics course and possibly the collections? Does this help? If not feel free to re post to this thread
Philip Holtzman
6,318 PointsThanks for your reply,
I did actually watch the videos several times, It is just syntax explanation and what the keywords mean. There is little talk on the fundamental programming logic required to solve the problems. I found several tutorials that were easier to build on logic, particularly Al Sweigards books which are free online, and include video tutorials.
you can find here: https://automatetheboringstuff.com/
I have seen several other people comment on my exact problem as far as logic goes, so I think that if the Python Course is to be successful the Logic side needs to be more thoroughly explained in order to help a beginner out
Josh Keenan
20,315 PointsAwesome, any particular terms I could help with today? I will also tag Kenneth Love so he can see this post and perhaps use this to update the courses! As far as I have found though, terminology throughout the whole computer science and programming spectrum is the same, with a few exceptions.
scott childs
974 PointsI would agree with Philip and add that there is something missing in the majority of programming classes for the beginner. I am currently going through three online courses, including this one, reading multiple books and even taking a community college course on Python and ALL of them are missing some key steps in teaching the fundamentals of thinking like a programmer. I am continually having to resort to the community help sites to find solutions and find many others struggling with the same problems who have much higher point accumulations than myself. The challenge is not having a visual sense of what is being presented. That visual is an association of logic steps in a language that we can see in everyday language. Its like teaching English as a second language--you do it with associative pictures. Kind of like Scratch or Blockly. After all, most of us are coming from a GUI framework that is almost all built on icons and images...and then we are expected to shift suddenly to command lines and word abbreviations with a huge relearning curve in memorizing, but not fully "seeing" what is going on with all the coded words.
Josh Keenan
20,315 PointsWhat specifics are you having problems with, both of you guys?
Philip Holtzman
6,318 PointsWell like I said, the syntax descriptions and theory behind all the programming concepts is great, it is the logic behind them? I understand what dicts, functions, and all the other keywords are.. But when the challenge happens, I don't know if the challenge is too difficult, but the task is requires a level of logic that i am unprepared for. It sort of assumes I would know how to plug this thing in there etc etc.. Having basically no practice with coding outside of a few very simple tasks to pass a value or function somewhere else.
In Learning Python the Hard Way and the book mentioned above, whether through handholding or lots of practice the logic becomes more clear, and the whole "thinking like a programmer" becomes slowly more clear.
I love these treehouse courses and I believe I can finish these challenges, but at this point I think unless I want to rely solely on the forums help, I would have to attempt some other tutorials/books first and come back to it
scott childs
974 PointsJosh, thank you for being persistent. So...maybe start with how you would go through the steps of solving a treehouse challenge to a kid? No computer jargon or fancy terms. Just picking apart a challenge that has multiple steps. Do you start with psuedo code? What does that specifically look like? It is one thing to find all the solutions in the many community helps. It is quite different to break it down into everyday non-assuming language that can be built on later with the keywords and functions that come later. There is something BIG missing between the challenge question and the code that comes later.
Josh Keenan
20,315 PointsI loved that book, it's sitting next to me on my desk actually. The challenges are there to see if you understood the logic in the last lesson with fundemental logic basics, for loops, if statements etc. I don't feel like you should find each one impossible, for me it was always a case of going back to the video with the challenge in mind and watching intently then spotting the part which refers to the challenge!
Josh Keenan
20,315 PointsI'll give you the generic steps to solving the problem!
Firstly get the question, I'll use an example!
Create a function named string_factory that accepts a list of dictionaries and a string. Return a new list build by using .format() on the string, filled in by each of the dictionaries in the list.
Here's the code that comes with it:
dicts = [
{'name': 'Michelangelo',
'food': 'PIZZA'},
{'name': 'Garfield',
'food': 'lasanga'},
{'name': 'Walter',
'food': 'pancakes'},
{'name': 'Galactus',
'food': 'worlds'}
]
string = "Hi, I'm {name} and I love to eat {food}!"
My first step is to break down the challenge into smaller problems:
create a function called string_factory
it takes 2 arguments, a list of dictionaries and a string
format the string with the items from each dictionary in the list
return the list of new strings
Now I try to break down each problem even further and perhaps rearrange the order of the steps I need to take:
================================================================================================
create a function called string_factory
it takes 2 arguments, a list of dictionaries and a string
Create a new list to store the new strings
Create a loop to go through the list of dictionaries AND format the string
Add each new string to the list
return the list of new strings
================================================================================================
And I now have an idea of how to solve the problem!
So let's solve it by completing it one step at a time:
def string_factory():
Now I'm going to add the arguments to it
def string_factory(my_dicts, my_string):
Now create a new list
def string_factory(my_dicts, my_string):
string_list = []
Now I want a loop that goes over/through a set of items within a list
I know a for loop would be best, this goes over/through items.
def string_factory(my_dicts, my_string):
string_list = []
for index in range(len(my_dicts)):
This loop goes over every item within the list of dictionaries
def string_factory(my_dicts, my_string):
string_list = []
for index in range(len(my_dicts)):
string_list.append(my_string.format(**my_dicts[index]))
Then we add the new string to the list, then we take all the items within the item at the position and format them into the string.
Finally we return the list of strings
def string_factory(my_dicts, my_string):
string_list = []
for index in range(len(my_dicts)):
string_list.append(my_string.format(**list[index]))
return string_list
Post again for any more info!
scott childs
974 PointsThank you Josh for your detailed and thoughtful reply. Let me study this and I'll let you know how I'm doing.
I did think of a question relating to the above: you said, "then we take all the items within the item at the position and format them into the string." Can you explain this more? Are those items in "index"? And what do you mean by "the position"? Many thanks!
Josh Keenan
20,315 PointsThen we add the new string to the list, then we take all the items within the item at the position and format them into the string.
Finally we return the list of strings
def string_factory(my_dicts, my_string):
string_list = []
for index in range(len(my_dicts)):
string_list.append(my_string.format(**list[index]))
return string_list
Okay, let's put that highlighted phrase into some plain english:
We are taking all the items within the dictionary at the current index and then formatting them into the string.
We go to position X within the array, this is a dictionary. So then we jump into the dictionary and get the items within it. We then format the string with these items!
By position I mean where in the list we are, so the index we are at. Whether it be the first one (which is index 0) or 3rd,(which us index 2); the reason the 3rd position is index 2 is because all lists start from position 0 not 1, so the first position is index 0, get it?
anhedonicblonde
3,133 PointsWhen I run this code in the challenge, I get "'type' object not subscriptable". I've changed the string and dictionary names to reflect what you've named them here in this example. The code is below. Have I made a transcription error that I am not seeing possibly? Or is there another reason why it would give me this error? Thanks!
my_dicts = [
{'name': 'Michelangelo',
'food': 'PIZZA'},
{'name': 'Garfield',
'food': 'lasanga'},
{'name': 'Walter',
'food': 'pancakes'},
{'name': 'Galactus',
'food': 'worlds'}
]
my_string = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(my_dicts, my_string):
string_list = []
for index in range(len(my_dicts)):
string_list.append(my_string.format(**list[index]))
return string_list
anhedonicblonde
3,133 PointsThank you so much for the explanation! It makes so much more sense now. This section is tricky for me and your help is making things clearer - thanks for taking the time to break it down for me. :)
anhedonicblonde
3,133 Pointsanhedonicblonde
3,133 PointsOh yes, please, I would love if you would explain :) thank you so much for posting the new code. I'm still trying to get the hang of this, and this challenge had me perplexed. Thanks for your help!
Josh Keenan
20,315 PointsJosh Keenan
20,315 PointsOkay so let's go through it line by line.
1 - Creating the function string_factory and giving it two parameters, a set of dictionaries and a string. We place these inside parentheses and make sure we end the line with a : colon to let python know a function starts here. (I say this as most of my errors come from non-closed parentheses or a lack of a colon..)
2 - Here we create a new list called list1, which is currently empty but it will store all of the formatted strings at the end of this
3 - We create a loop that will go through each individual dictionary in our list of them.
Let's look at this a bit as it is an important part of programming, we can use a while or for loop for this. A while loop does something until a condition is met, a for loop iterates (goes over/through) every item within something until there is nothing left to see. So a for loop would be the best.
4 - This is the line that formats the string, I'll explain it in pieces and summarise at the end.
Firstly, we get list1 and call the append method to append each item to it, basically we just add whatever the string is to the end of the list.
Then we get the string and call the format method, allowing us to place the value of each key in at the right spot in the list.
In the parentheses we set the first item being formatted, name, to the value of the name key of the current dictionary, and then do the same for the food item
5 - Finally we have now finished looping over every item and have a full list of complete strings, so we return them.
So what did we just do?
We created a function called string_factory that takes a list of dictionaries and a string as parameters, it then creates a list of strings formatted with the values of each dictionary and then returns a list of them.
I hope this helped!:)