Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Disemvoweled

Shadow Skillz
Shadow Skillz
3,020 Points

Same script different output?

Created the script(to my knowledge) as the tutorial but when i run the program instead of giving me one list minus the vowels it gives me a list that icramentaly removes the vowels giving variation of the words its working on until done, not to sure what I'm missing.

state_names = ["Alabama", "California","Oklahoma","Florida"]
vowels = list('aeiou')
output = []


for state in state_names:
  state_list = list(state.lower())

  for vowel in vowels:
    while True:
      try:
        state_list.remove(vowel)
      except:
        break
    output.append(''.join(state_list).capitalize())

print(output)

the result is this

['Lbm', 'Lbm', 'Lbm', 'Lbm', 'Lbm', 'Cliforni', 'Cliforni', 'Clforn', 'Clfrn', 'Clfrn', 'Oklhom', 'Oklhom', 'Oklhom', 'Klhm', 'Kl hm', 'Florid', 'Florid', 'Flord', 'Flrd', 'Flrd']

3 Answers

Hi Christian,

I found the issue in your code, you are correct the code is mostly right and is exactly like the solution, however you made a spacing mistake. These mistakes are common in python I have to watch out for them myself. In your code:

output.append(''.join(state_list).capitalize())

It is under the while true, when should be under for vowel in vowels:, so your output.append is being executed too many times because it's under the while True section instead of for vowel in vowels:

To fix this all you need to do is go to the beginning of the output.append statement and delete one tab space so that it lines up with for vowel in vowels: part of the code, then your answer will look exactly like the tutorial's solution. For example:

def main():
    state_names = ["Alabama","California","Oklahoma","Florida"]
    vowels = list("aeiou")
    output = []

    for state in state_names:
        state_list = list(state.lower())

        for vowel in vowels:
            while True:
                try:
                    state_list.remove(vowel)
                except:
                    break
        output.append("".join(state_list).capitalize())


    print(output)
main()

Yeah, my answer was wrong. Whoops! Look like learning JavaScript mixed up my Python knowledge.

Shadow Skillz
Shadow Skillz
3,020 Points

Thanks for the help Nicholas ;-)

I think you need to unindent the output.append line, so that it matches the 'break' line. Without that indent, the word will get appended every time a vowel is removed, which is why there is a lot of repeats in your final list.

Shadow Skillz
Shadow Skillz
3,020 Points

Kev thanks for helping too i need all i can get bro i appreciate it ;-)

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Christian.

I have edited your question so that it matches these guidelines, please have a read: https://teamtreehouse.com/forum/posting-code-to-the-forum

Getting to your question: Are you sure the code is the same? First thing I have noticed for example is that the indentation of the line starting with output.append does not match the indentation of the video's code.

That could be a starting point to debug the code...

;)

Let me know if you need more help!

Vittorio

Shadow Skillz
Shadow Skillz
3,020 Points

Thanks for the help Vittorio really appreciate it. These eye's decieve me every now and then ;-)