Printing inside functions


#1

I'm stuck here:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

Variable a won't print when I run the code. Why?
I also don't understand why the written code won't append all words to wordlist and replace them with astericks as intended. I get a shorter output: ['hey', 'hey'] instead of "*** *** ***".

def censor(text,word):
    a=0
    variable=text
    wordlist=[]
    wordstring=''
    for c in variable:
        a+=1
        if c.isspace()==False:
            wordstring+=c
        else:
            wordlist.append(wordstring)
            wordstring=''
        
    for obj in wordlist:
        if obj==word:
            for c in obj:
                c='*'
        print a
    return wordlist


#2

If you want to print inside a function, which is a great way to debug it, you will need to call the function yourself. For whatever reason when code academy calls it in their check it does not seem to be done in the interpreter to the right.
Add this line to the end of your code:
print censor('Unicorns burp fairy dust!', 'burp')
Adding additional print statements within will help you find where your code goes wrong.
I went ahead and did this for you below in a spoiler, but you may want to try it for yourself first :wink:


def censor(text,word):
a=0
variable=text
wordlist=[]
wordstring=''
for c in variable:
a+=1
if c.isspace()==False:
print 'in if'
print c
wordstring+=c
else:
print 'in else'
print wordstring
wordlist.append(wordstring)
wordstring=''

for obj in wordlist:
print 'in for'
print obj
if obj==word:
print ' obj = word!'
print obj
for c in obj:
print 'in second for'
print c
c='*'
print a
return wordlist
print censor('Unicorns burp fairy dust!', 'burp')


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.