Search code examples
pythoncpu-wordsentence

Words without Vowels


In this code basically I'm trying to count those words in this sentence in which there is no vowel but there is something (or maybe everything) that I'm doing wrong, here is the code

par="zyz how are you"
count=0

for i in range(len(par)):
    if par[i]==" ":
        if par[i]!="a" or par[i]!="e" or par[i]!="i" or par[i]!="o" or par[i]!="u":
            count+=1
        
print("total words without vowel -> ",count)

Solution

  • When you use len(par), it returns how much letters is in the string. Instead you have to split the string word by word by using par = "zyz how are you".split(" ")

    After spliting, you would get par as a list which contains ["zyz","how","are","you"]

    Now you can just check if there is a vowel in the word, instead of looping through every letter

    par = "zyz how are you".split(" ")
    count = 0
    
    for i in range(len(par)):
        if "a" in par[i] or "e" in par[i] or "i" in par[i] or "o" in par[i] or "u" in par[i]:
            pass
        else:
            count += 1
    
    print("total words without vowel ->",count)