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 trialSachin Gupta
130 PointsHow to solve this? How can I process a local variable outside its scope?
Suppose I want to count the words in a loop and print it outside the loop, I am not able to access it? Please help me to debug this code.
def word_count(string):
mydict = {}
mylist = string.split()
for item in mylist:
count = 0
for key in string:
if(key == item):
count += 1
mydict[item] = count
return mydict
dict = word_count("Racism is bad is really bad")
print dict
[MOD: added ```python formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsIn your code there are two scopes: the main (top), and inside word_count
. Code inside word_count
can access variables in both of these namespaces, code outside of word_count
can only access the variables in the global namespace.
Global name space: word_count
function, dict
dictionary
Local word_count
namespace: mydict
dictionary, mylist
list, count
int, key
string.
Within word_count
, after the loops have run, but before the return
, all of the local variables have their last value.
The last value of key
is "d", count
is 0. Why is key
last value equal to "d"? There are some issues with your code. You are comparing the characters in the string to the words in the string. Will only match single character words.
def word_count(string):
mydict = {} #<-- no need to initialize dict
# split string into word list
mylist = string.split()
# loop using each word in wordlist
for item in mylist:
# Initialize count
count = 0
# loop over each character in string
for key in string: #<-- not what you want: try using "string.split()" or "mylist"
if key == item: #<-- parens not needed
# increment count for each matching word
count += 1
# update dict with word count results
mydict[item] = count
# return dict of all results
return mydict
dict = word_count("Racism is bad is really bad")
print dict
Notice this is a bit inefficient as you are scanning the word list with every word in the list. So you would be counting all the "is" words twice. Can you think of a way to loop through the word list one and simply increment the count of each word seen? Remember to test if the word has been see before (hint: try "if item in mydict" to see if the word is there. increment if yes, create new dict item using mydict[item] = 1
)
Sachin Gupta
130 PointsSachin Gupta
130 PointsThank you Chris ! I implemented your suggestions and it worked ! But I am still not able to figure out how can I use try and except here. My improved code is :
Thank you once again!!
[MOD: wrapped code in ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsHere is how you can use a
try
statement:Returns
{'!': 1, 'This': 1, 'is': 2, 'it': 1, 'Answer': 1, 'Awesome': 2, 'Really': 1}