How can I ameliorate this program that counts vowel? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I ameliorate this program that counts vowel?

https://code.sololearn.com/c8Eml2BrB5Eh/?ref=app

17th Aug 2022, 9:59 AM
eloufou
eloufou - avatar
19 Answers
0
You'll go far eloufou
17th Aug 2022, 11:30 AM
Aaron ngetich
Aaron ngetich - avatar
+ 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))
17th Aug 2022, 10:20 AM
A͢J
A͢J - avatar
+ 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 😁👍
17th Aug 2022, 11:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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
17th Aug 2022, 11:27 AM
eloufou
eloufou - avatar
+ 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']))
18th Aug 2022, 1:29 AM
Bob_Li
Bob_Li - avatar
+ 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
17th Aug 2022, 10:47 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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))
17th Aug 2022, 10:20 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 2
Hi! if the program works, you should not improve. can break something by accident
17th Aug 2022, 10:16 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Yaroslav Vernigora Alright, thx
17th Aug 2022, 10:19 AM
eloufou
eloufou - avatar
+ 2
https://code.sololearn.com/cBU3UmDV1ixW/?ref=app Check this mine I think is more clear and simple
17th Aug 2022, 11:07 AM
Abdurrazak Ridwan Ahmad
Abdurrazak Ridwan Ahmad - avatar
+ 2
Rik Wittkopp special regards sir You also made this same correction to me🌹🥰
17th Aug 2022, 11:19 AM
Abdurrazak Ridwan Ahmad
Abdurrazak Ridwan Ahmad - avatar
17th Aug 2022, 11:20 AM
eloufou
eloufou - avatar
+ 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))
17th Aug 2022, 6:21 PM
Aquib
Aquib - avatar
+ 2
If you want it to be 1 line long, here is my code https://code.sololearn.com/c820zglb2UXX/?ref=app
18th Aug 2022, 12:41 AM
Some Guy Whit A Computer
Some Guy Whit A Computer - avatar
+ 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…
17th Aug 2022, 10:28 AM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
+ 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}\'")
17th Aug 2022, 10:50 AM
Aaron ngetich
Aaron ngetich - avatar
+ 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!
17th Aug 2022, 10:57 AM
eloufou
eloufou - avatar
+ 1
Rik Wittkopp thanks, that will help alot.
17th Aug 2022, 10:57 AM
Aaron ngetich
Aaron ngetich - avatar
+ 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
19th Aug 2022, 4:57 AM
Fynn Nix
Fynn Nix - avatar