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 trialCaitlin Cadieux
2,374 PointsPrints out multiple times
I retyped the code in the answer to this question, but my result is slightly different.
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)
When I run this code, it prints the following:
['Lbm'] ['Lbm', 'Clfrn'] ['Lbm', 'Clfrn', 'Klhm'] ['Lbm', 'Clfrn', 'Klhm', 'Flrd']
When the instructor runs the same code (as far as I can tell), only the last line prints. Why am I getting this result?
3 Answers
ML Johnson
3,722 PointsUnfortunately, I can't answer the question as I'm getting the same thing
I too am having the same problem, new attempt on a new day. Today, I tried to copy KL character for character and still got the multiples - and I've checked to make sure my print call is not indented (At least I think I have.) Is this an issue with Workspaces?
state_names = ['California', 'Florida', 'Arkansas', 'Colorado']
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)
Console
treehouse:~/workspace$ python devowel.py
['Cliforni', 'Cliforni', 'Clforn', 'Clfrn', 'Clfrn', 'Florid', 'Florid', 'Flord', 'Flrd', 'Flrd', 'Rknss', 'Rknss', 'Rkns
s', 'Rknss', 'Rknss', 'Colordo', 'Colordo', 'Colordo', 'Clrd', 'Clrd']
Alex Wheeldon
7,305 PointsWell, first of all, both of these are great ways to do this challenge, and the good news is that these both involve (very) easy fixes. Because Python is indentation sensitive, it can be tricky to line everything up correctly. In this case, when you store your results in your 'output' arrays, you are calling it within your first for loop. This is causing results to be appended multiple times, and giving you a weird result. I have tried both solutions and if you de-dent the output.append lines in your code, you should be fine. Happy coding!
Mustafa Başaran
28,046 PointsHi Caitlin, Hi Mlj,
Your problem is mainly due to the fact that you are appending the same items to output in each iteration of the second for loop while you need to do this at the end of the first for loop instead. So, you need to indent your code correctly... This may sound a bit complicated. So, here is my code with some comments that I thought that would be useful for you.
I hope that this works for you.
wovels = list('aeiou')
state_list = ['Alabama', 'California', 'Minnesota', 'Ohio', 'Nebraska', 'Washington', 'Connecticut', 'Idaho']
output = []
for state in state_list:
# In this loop we are creating an intermediary list to store and modify
# the elements of the original list.
# Just fill it with all state names uncapitalized.
state_list_intermediary = list(state.lower())
for wov in wovels:
# Running a second for loop within the first loop.
# Until there is no wovel left, remove it from the intermediary list
# we created in the first loop
while True:
try:
state_list_intermediary.remove(wov)
except:
break
# Here we are out of the second for loop and back to the scope of the first for loop.
# Append the modified elements of the intermediary lists capitalized to the empty output list.
output.append(''.join(state_list_intermediary).capitalize())
# Out of the first for loop. Just print the output list.
print(output)