New course! Every coder should learn Generative AI!
Try a free lesson0
What is wrong with my code ?
#counting number of vowels word = str(input("Enter a word : " )) vowel = "a" or "e" or "i" or "o" or "u" numvowel=0 for vowel in word : numvowel +=1 print ("Number of vowels : " , numvowel)
3 Answers
+ 8
1. Unnecessary str cast on input, which is already of a string class.
2. vowel variable should be defined as a collection, not as a logical expression (which gets resolved right away)
+ 5
1. input() returns a string - no need to cast str on it
2. You could define vowel as a collection, like a list, tuple or even a string, to be able to iterate through it in your for loop. Now it is a logical expression which gets resolved to "a" (it is the first one which is True and so 'or' takes it right away) -- this is not the case for using 'or' (or any other logical expression for that matter).
0
@Kuba Siekierzynski .. can you explain my mistake a bit more ?