6 Antworten
+ 11
# Just use "Copy text" and paste it into Python CodePlayground
# If you want to do it the smartest way - import re and find and match vowels and consonants.
# However, you may also create a vowels list by hand (as it's shorter than consonants ;) and iterate through the given string adding 1 whenever the currently iterated element is in the vowels list.
# Like this:
vowels = 'aeiou' # 'y' is iffy in English ;)
s = 'AlohaNamaSayaBudi'
v = 0 # v will act as a vowel counter
for ch in s: # iterating through the string
if ch.lower() in vowels:
v += 1 # increase v by one if a vowel is spotted
print('{} has {} vowels and {} consonants.'.format(s, v, len(s)-v))
# prints the string, the vowels and the number equal to length minus vowels (assuming there are no spaces or special characters)
+ 10
@Ace Very True, but it came to me unexpectedly on an English test ;) Had never really thought about it and I assumed of course it's a vowel, just like in Polish. Zonk! Nope. :)
+ 5
By vocal, I think you mean vowel.
vowel = 0
consonant = 0
for letter in 'AlohaNamaSayaBudi'.lower():
if letter in 'aeiou':
vowel += 1
else:
consonant += 1
print(vowel)
print(consonant)
0
waw Thanks @ChaoticDawg @Ace @Kuba Siemierzyński i will try all your code