+ 1

The code doesn't work

The code is supposed to count the number of vowels in a string and it doesn't work? x = input("Enter a string:") z = 0 y =0 for i in x: if i== 'a' or 'A': y +=1 elif i == 'e' or 'E': y +=1 elif i == 'i' or 'I': y +=1 elif i == 'o' or 'O': y +=1 elif i== 'u' or 'U': y +=1 else: z = 0 print("the output:", y)

9th Jan 2024, 2:31 PM
Infernyx
Infernyx - avatar
5 Réponses
+ 11
You are using `i == 'a' or 'A'`. It's corrected form is `i == 'a' or i == 'A'` as I do below. https://sololearn.com/compiler-playground/cgpyxr4Y3HpY/?ref=app
9th Jan 2024, 2:41 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 6
It should be if i == 'a' or i == 'A'. An alternative is if i in 'aA' or much simpler if i in 'aeiouAEIOU'.
9th Jan 2024, 2:41 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 6
Infernyx , > your code defines / initialize a variable `z` with 0. > in the `else` clause the value of 0 will be assigned to variable `z`. > but this varisble is never used.
9th Jan 2024, 4:54 PM
Lothar
Lothar - avatar
+ 2
Or turn the i to lower case and get rid of all or statements for i in x: i = i.lower() if(i in "aeiou"): y += 1
11th Jan 2024, 4:07 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
Thank you for helping me people
10th Jan 2024, 3:00 PM
Infernyx
Infernyx - avatar