What is wrong with my code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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)

18th Jun 2018, 6:29 AM
Arunava Sarkar
Arunava Sarkar - avatar
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)
18th Jun 2018, 6:38 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 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).
18th Jun 2018, 7:08 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
@Kuba Siekierzynski .. can you explain my mistake a bit more ?
18th Jun 2018, 6:47 AM
Arunava Sarkar
Arunava Sarkar - avatar