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

Python Python Collections (2016, retired 2019) Lists Disemvowel

Musashi Schroeder
Musashi Schroeder
4,289 Points

Curious about the interaction between the return statement and print

I realize the code doesn't work properly because of the list iteration and the way it manipulates it, but I noticed whenever I would return 'word', I would end up getting "whaaat" instead of "what", which should have been the incorrect answer. When I change the print statement to print(disemvowel(word)), it works fine.

Why doesn't calling the function then printing, result in "what"?

disemvowel.py
def disemvowel(word):
    i = 0
    word_list = list(word)
    while i < len(word_list):
        if word_list[i] == "a":
            del(word_list[i])
        i += 1
    word = "".join(word_list)
    return word

word = "whaaat"
disemvowel(word)
print(word)

2 Answers

You need to assign the function to word in order to update word. Try the following:

word = "whaaat"
word = disemvowel(word)
print(word)
Musashi Schroeder
Musashi Schroeder
4,289 Points

Thanks, yeah. Took a break and realized my mistake :/

It's scope issue, the word variable has declared as a global variable, so you have to override it word = "whaaat" word = disemvowel(word) print(word)

or you can use print the disemvowel function instead of return as the following

def disemvowel(word): i = 0 word_list = list(word) while i < len(word_list): if word_list[i] == "a": del(word_list[i]) i += 1 word = "".join(word_list) print(word)

word = "whaaat" disemvowel(word)