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 trialAshmit Pathak
4,441 PointsPlz tell how to do this
Plz send me the correct code to solve this problem
def disemvowel(word):
return word
vowel = ["a" , "i", "e","o","u"]
try:
word.remove(vowel)
except ValueError:
pass
1 Answer
Alexander Davison
65,469 PointsHello Ashmit,
You seem a little lost.
- All of the code you write should be within the
disemvowel
function. - You cannot remove
vowel
fromword
, asword
does not contain a list (it's a string!). - You do not need a try/except block for this problem.
Unfortunately, as I have stated, you cannot remove an array from a string. It simply doesn't make sense. Moreover, even if you remove a single character from a string, it only would remove the first find for that character, it wouldn't remove all finds.
This means you must iterate through the string, and if the character you're on isn't in the string "aeiou"
, then you may append the character to the result string which you will return.
Here's the code, but please do not copy and paste the code, just understand how it works. Trust me, if you type it out on your own, you will learn programming much better.
def disemvowel(word):
result = ''
for i in word:
if i not in "aeiouAEIOU":
result += i
return result
Happy coding! ~Alex
UPDATED DUE TO MISTAKE IN CODE
Ashmit Pathak
4,441 PointsAshmit Pathak
4,441 Pointssir why have to take this step result += i in the code plz explain
Alexander Davison
65,469 PointsAlexander Davison
65,469 Pointsresult += i
appendsi
to the end of the stringresult
.For example...
(Pretend I'm in the Python Shell.)
It's actually a shorthand for this:
result = result + i
And, as you may know,
result
andi
are both strings, and in Python, when you add two strings together, it "glues" them together.I hope this helps