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 trialLaknath Gunathilake
1,860 Pointstrying to add a section, so that users can input state names
I'm trying to add a section, so that the user can input states to the list
<p>
state_names=["Alabama","California","Oregon","Laknath"]
vowels=list('aeiou')
output=[]
while True:
state_names.append(input('>'))
continue
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)
<\p>,,,
1 Answer
Matthew Hill
7,799 PointsYour while True and continue here creates an infite loop. While True will always be True, and continue will always take you back to the start of your loop, avoiding all the code you've entered below:
<p>
while True:
state_names.append(input('>'))
continue
</p>
I can think of three ways you could do this, but I'll only show you one. First, you can add a count that is reduced each time you go through the loop (i.e. the user must enter excatly 5 states). Second, you could simply remove the loop so the user only adds 1 state. Third, you could try this:
<p>
while True:
temp_string = (input("Enter a state, or 'D' when done: "))
if temp_string[0].lower() == "d":
break
state_names.append(temp_string)
</p>
Žiga Pregelj
18,126 PointsŽiga Pregelj
18,126 PointsOn the first and quick look i gave, i think it would be better if you put your
outside of the While loop, so that it would ask you first what states do you want to append to state_names and then proceed to the While loop and remove vowels from that states.