-2

This program is supposed to show the different types of characters in a string entered by the user but for some unknown reason it doesn't show the number of vowels in a string even when there are a bunch of them in it. If someone could identify the mistake it would be great!

str=input("Enter a string: ")
upper   = 0
lower   = 0
num     = 0
vowel   = 0
special = 0
for i in range(len(str)):
  if(str[i].isupper()):
     upper+=1
  elif(str[i].islower()):
     lower+=1
  elif(str[i].isdigit()):
      num+=1
  elif(str[i] == "a"or"e"or"i"or"o"or"u"or"A"or"E"or"I"or"O"or"U"):
      vowel = vowel+1
  else:
      special+=1
print("Upper Case Letters: ",upper)
print("lower case letters: ",lower)
print("Numbers: ",num)
print("Vowels: ",vowel)
print("Special Characters: ",special)
  • it will end on the first `if elif`. replace all `elif` with `if` and you should be good to go. – Mahrkeenerh Oct 07 '21 at 13:34
  • `str[i] == "a"or"e"or"i"or"o"or"u"or"A"or"E"or"I"or"O"or"U"` is not how you compare `str[i]` against multiple alternatives – ForceBru Oct 07 '21 at 13:34
  • `elif` is only checked if the preceding if conditions did not match. So you're only checking if it might be a vowel if your previous checks established that the character is not an upper or lower case letter. – khelwood Oct 07 '21 at 13:35
  • The duplicate only solves one of the problems in your code. – khelwood Oct 07 '21 at 13:37
  • And by the way, don't use `str` as your variable name: it's the name of a built-in type. – khelwood Oct 07 '21 at 13:56

2 Answers2

0

First you need to replace all elif's with if's, then:

Replace

elif(str[i] == "a"or"e"or"i"or"o"or"u"or"A"or"E"or"I"or"O"or"U"):

with:

if (str[i] in ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]):
paolostyle
  • 1,849
  • 14
  • 17
  • Or `if str[i] in 'aeiouAEIOU':`. And it would be even better not to use index access and to iterate over the characters of the string instead. So we replace `for i in range(len(str)):` by `for char in str:` followed by comparisons like `if char.isupper():`. – Matthias Oct 07 '21 at 13:45
-2

Its because there is no space between the "or" and the next string, in python a r"" is considered a 'raw string'.

MK14
  • 481
  • 3
  • 9