0

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)
Bilal Razi
  • 21
  • 6
  • This is a fundamental logical error which you can understand by a) looking at the linked duplicate; or b) looking up "de Morgan's laws" on Wikipedia or with a search engine; or c) by carefully tracing through the logic by hand to see what happens when `par[i]` is a vowel. – Karl Knechtel Mar 20 '21 at 19:38
  • But that still only counts the non-vowel *characters*; the only way your code will care about *words* is if you have something in your code that tries to *distinguish* the words. – Karl Knechtel Mar 20 '21 at 19:40
  • I wanted to link you a duplicate for "how do I split a string into words?", but everyone I can find asking about it is asking a more complicated question. – Karl Knechtel Mar 20 '21 at 19:42

1 Answers1

3

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)
Dav_Did
  • 188
  • 10
  • par = "zyz how are you".split(" ") Oh! so basically in this split function it is splitting every letter before it finds the space right? – Bilal Razi Mar 20 '21 at 19:32
  • It sort of like it divide the string by the spaces, of course, you can use it to split by many other character as well. – Dav_Did Mar 20 '21 at 19:34