-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exercise 1 python. #8
base: main
Are you sure you want to change the base?
Conversation
python/exercise1/vowel_counter.py
Outdated
for letter in text: | ||
if letter not in vowels: | ||
print("consonant", letter) | ||
num += text.lower().count(letter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing num to num1 can differentiate it from the num used for identifying the vowels.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, copying the code used to count the vowels and then changing the vowels out for consonants may be an easier fix:
def num_vowels(text):
"""Return the number of vowels in string."""
vowels = "aeiouy"
num = 0
for v in vowels:
num += text.lower().count(v)
return num
"""Return the number of consonants in string."""
def num_consonants(text):
consonants = "bcdfghjklmnpqrstvwxz"
num1 = 0
for c in consonants:
num1 += text.lower().count(c)
return num1
text = str(input("Enter a sentence: "))
print("Number of vowels", num_vowels(text))
print("Number of consonants", num_consonants(text))
Tested with the following strings:
|
changed 'vowels' to 'consonants'
delete line
changed 'letter' to 'c'
print the values of interest for vowels and consonants
Description
Added some comments explaining the code.
Fixed the consonant printing.
Improved the message that is printed out.
Tested with the following strings:
Fixes issue
Fixes #7 python