I am working on a project that I have to get user input, put it into a list, pass that list to multiple funcitons for manipulation, and then return the data as new lists. I was originally having trouble getting the lists to pass without altering the original list until I found out I was able to use .copy(). Well, now that I have "passed" the original list down to the functions I am using to manipulate the user input data, the manipulations for sorting the input by most and least amount of vowels are no longer working.
import sys
def main():
words = []
wordCount = 0
userWord = input("Enter at least 8 words or 'bye' to leave the program: ").split(' ')
while True:
if len(userWord)<8:
print("Please print at least 8 words, try again.")
sys.exit()
elif wordCount<=8 and userWord[wordCount] != 'bye':
words.append(userWord[wordCount])
wordCount = wordCount + 1
else:
break
def most_vowels(): #broken - not sorting by most amount of vowels - giving original list
words4 = words.copy()
sorted (words4, key = lambda word: sum(ch in 'aeiou' for ch in word),
reverse = True)
print ('Your list of words sorted by most amount of vowels: ',words4)
most_vowels()
def least_vowels(): #broken - not sorting by most amount of vowels - giving original list
words5 = words.copy()
sorted (words5, key = lambda word: sum(ch in 'aeiou' for ch in word))
print ('Your list of words sorted by least amount of vowels: ',words5)
least_vowels()
main()
Prior to adding the words4 = words.copy() and words5 = words.copy(), both of these functions worked exactly like they were suppose to. Since I added this in, they are no longer working and they now just output the original list in its original order instead of sorting the words by most amount of vowels and least amount of vowels. Can anyone tell me what I am doing wrong? Thank you!