Feb-10-2020, 05:48 PM
Hello,
I'm finding it difficult to understand the behaviour of the while loop inside the for loop. To be more exact, I don't understand why the output.append function is able to gather all the words, even if it's inside the for loop. So here is the code:
Thank you!
I'm finding it difficult to understand the behaviour of the while loop inside the for loop. To be more exact, I don't understand why the output.append function is able to gather all the words, even if it's inside the for loop. So here is the code:
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)So I would basically except output.append to work just like it would work if it were outside the for loop, that is to say, the to output only the last word without the vowels:
['Flrd']But somehow all the words are added, and I'm not sure how this is happening, if there is no statement inside the while loop. Can anyone take this apart for me?
Thank you!