+ 2
How can I ameliorate this program that counts vowel?
19 odpowiedzi
0
You'll go far eloufou
+ 11
eloufou
'Y' or 'y' is not vowel
string = input().lower()
li = ['a', 'e', 'i', 'o', 'u']
total = 0
for i in string:
for j in li:
total += i.count(j)
print(string, "\nnumber of vowel:", total)
----------------------------
string = input().lower()
li = ['a', 'e', 'i', 'o', 'u']
z = [x for x in string for y in li if x == y]
print(string, "\nnumber vowel:", len(z))
+ 8
eloufou
A1.
.lower() will convert all letters in the string to lower case as you surmised.
If you look at the optional txt, then you will see a Capital 'A', which is converted to 'a' and thus recognised by the filter.
A2.
for letter in set(txt):
Instead of inspecting every letter in the txt, I create a set(txt).
So "this is a big test" is reduced to "thisabge", which makes it quicker.
If letter in vowels:
As we inspect each letter of the set(txt), we check to see if it is a vowel.
If it is a vowel, we move on to the next line of code to do something.
sumVowels += txt.count(letter)
This is the action that happens when a vowel is identified.
letter is the iterated item being inspected, so the count() of that letter in txt is added to the variable sumVowels.
Hope this makes sense 😁👍
+ 6
Aaron ngetich Abdurrazak Ridwan Ahmad A͢J abpatrick catkilltsoi Rik Wittkopp Yaroslav Vernigora
The thing that you surely don't know is that I started learning Python Lang 2 hours ago so if I'm not wrong, I think my first is not that bad... but still thx for your help. Your small action created big help for me
+ 6
eloufou
😁 vowel counting party again! 🎈🎉🎊🎇
Great work. If it works, it is good.😎
You have the proper problem-solving mindset of a coder. 🤓
But we are coders, not novelists, so sooner or later, you will try to streamline your code.
After you learn loops, string methods and list comprehension, here is my suggestion:
string = input()
vowels = 'aeiouAEIOU'
print(string, '\nnumber of vowels:', sum([string.count(c) for c in vowels]))
#or if you only want vowel count:
print(len([c for c in string if c in 'aeiouAEIOU']))
+ 4
eloufou
Here is an example using your concept where you get the code to do all the work, instead of hard coding every item.
https://code.sololearn.com/c4j9sFHb8GkV/?ref=app
+ 3
Hey. Always try to improve the code to make it better. Here is how I remade it:
(I am sure it can be done better but I don't really know Python)
string = input()
list(string)
count=0
total=0
vocals = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
while count<len(vocals):
total+= string.count(vocals[count])
count+=1
print(string + """
""" + "number of vowel: " + str(total))
+ 2
Hi! if the program works, you should not improve. can break something by accident
+ 2
Yaroslav Vernigora
Alright, thx
+ 2
https://code.sololearn.com/cBU3UmDV1ixW/?ref=app
Check this mine
I think is more clear and simple
+ 2
Rik Wittkopp special regards sir
You also made this same correction to me🌹🥰
+ 2
Rik Wittkopp
Thx
+ 2
string = input().lower()
sumVowel=0
for x in string:
if x=='a':
sumVowel+=1
if x=='e':
sumVowel+=1
if x=='i':
sumVowel+=1
if x=='o':
sumVowel+=1
if x=='u':
sumVowel+=1
print(string + """
""" + "number of vowel: " + str(sumVowel))
+ 2
If you want it to be 1 line long, here is my code
https://code.sololearn.com/c820zglb2UXX/?ref=app
+ 1
Naming the variable as string is not a good idea…if you want to indicate it is string, better replace with str_word etc…
+ 1
# lets count vowels
v = ['a','A', 'e', 'E', 'i', 'I', 'o', 'O', 'u','U']
count = 0
x = input('Enter any words to count vowels: \n')
for i in v:
for j in x:
count += i.count(j)
print (f"there are {count} vowels in \'{x}\'")
+ 1
Rik Wittkopp
Thx for this! I understand almost all of the code and it's way simpler than mine!
1st question:
The '.lower()' is a function to make all the character in the list on lowercase? Is that it or it's something else?
2s question
Can you explain what the frick is happening in the for loop where its calculating the amount of vowel?
3rd question
how does the for loop check if the character that is being check is a vowel by checking a String value? Char and String are compatible in Python?
Thx again. Your the man!
+ 1
Rik Wittkopp thanks, that will help alot.
+ 1
I put together a few methods that would count vowels. I also test their execution times and compare those. You may try it yourself :)
https://code.sololearn.com/c7nBF09ppuLm/?ref=app