-2
vowels = 'aeiouAEIOU'

count = 0

text = input()

while text != "end":
    for i in text: 
         if i in vowels: 
            count += 1
            
    count  
    text = input()
    
#print(count)   

I am not sure how to structure the code but please see below:

EXAMPLE INPUT:

input 1:I LOVE YOU
input 2:TEST
input 3:YOU
END

EXAMPLE OUTPUT:

5
1
2

The output should be the number of vowels in each input as per above example.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kholofelo
  • 11
  • 3
  • vowels='aeiouAEIOU' count=0 text = input() while text != "end": for i in text: if i in vowels: count += 1 count text = input() #print(count) – Kholofelo Apr 09 '21 at 14:51
  • Although there is no question here, I am guessing that your problem is that you need to move the `count = 0` line inside the `while` loop... – Tomerikoo Apr 09 '21 at 14:54
  • Your question seems to stem from a fundamental misunderstanding of the basic constructs of the language. I'm not sure how you expect the the current implementation to print something for each input without having a print statement within the loop itself. A beginner's tutorial in Python will probably answer your question better than anyone here. – mario_sunny Apr 09 '21 at 15:43

1 Answers1

2

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
Galunid
  • 578
  • 6
  • 14
  • Code-only answers to code-only questions really puts down the bar of quality this site strives to. First of all try to only answer well-asked questions, and further if you decide to answer them, at least add some explanations to your answer to make them a bit more valuable... – Tomerikoo Apr 09 '21 at 14:56
  • Why do you print the `text` along with the `count`? – Roy Cohen Apr 09 '21 at 14:57
  • 1
    I agree, I believe comments included in the answer should provide necessary context. – Galunid Apr 09 '21 at 14:58
  • @Tomerikoo I agree that *more* explanation is needed, but saying there's no explanation at all is inaccurate as there's explanation in the form of comments. – Roy Cohen Apr 09 '21 at 15:00
  • 1
    @RoyCohen There wasn't really any reason, other than my preference. I edited the answer to provide a bit more explanation and removed the unneeded print – Galunid Apr 09 '21 at 15:04
  • Thank you this. how would i input all my text and only get each lines number of vowels after the end – Kholofelo Apr 09 '21 at 15:06
  • @Kholofelo Start by editing your question to explain that. Provide a few example inputs and how you expect your program to work with them – Tomerikoo Apr 09 '21 at 15:12
  • @Kholofelo I edited the answer to show how to do this. Please try to edit your question to explain what you want more clearly and ask more directed questions in the future – Galunid Apr 09 '21 at 15:14
  • AS IN HOW WOULD I GET THE ELOW: EXAMPLE INPUT: I LOVE YOU TEST YOU END EXAMPLE OUTPUT: 5 1 2 – Kholofelo Apr 09 '21 at 15:15
  • @Kholofelo Please see if the final edit solves your doubts – Galunid Apr 09 '21 at 15:26
  • it solves it. than you very much – Kholofelo Apr 09 '21 at 15:29