One solution would be to create infinite loop (while True
) and break out of it when we encounter word "end"
vowels = 'aeiouAEIOU'
while True:
text = input() # it will get a new value every time the loop executes
count = 0 # note it'll be assigned 0 every iteration
if text == "end":
break # end the loop, since we found the word "end"
# if we get here, it means the word is not "end"
for letter in text:
if letter in vowels: # letter is a vowel, increase count by 1
count += 1
print(count)
To first read all the text and only then count/print the result you could first collect all the strings in a list
.
vowels = 'aeiouAEIOU'
user_text_list = [] # we create empty list
while True:
text = input() # get input from user
if text == "end":
break # end the loop, since we found the word "end"
# if we get here, it means the word is not "end"
user_text_list.append(text) # "save" the word in a list
# once we get here, it means we encountered the word "end" and we can count the vowels
for word in user_text_list: # for each element of the user_text_list (user inputted word)
count = 0 # initialize count to 0 every iteration
for letter in word:
if letter in vowels: # letter is a vowel, increase count by 1
count += 1
print(word, count) # print word and vowel count
There isn't really an easy way to do it. Python waits for user to press [ENTER]
when using input
. If that's what you want, please refer to this question.
wordlist = []
while True:
text = input()
text = text.split(" ") # converts "I LOVE YOU" to ["I", "LOVE", "YOU"]
for word in text:
if word == "end":
break
wordlist.append(word) # if word is not "end", add word to list
for word in wordlist: # for each element of the user_text_list (user inputted word)
count = 0 # initialize count to 0 every iteration
for letter in word:
if letter in vowels: # letter is a vowel, increase count by 1
count += 1
print(word, count) # print word and vowel count