Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
8 changes: 5 additions & 3 deletions python/exercise1/vowel_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

def num_vowels(text):
"""Return the number of vowels in string."""
vowels = "aeiou"
vowels = "aeiouy"
num = 0
for v in vowels:
num += text.lower().count(v)
return num

def num_consonants(text):
vowels = "aeiou"
vowels = "aeiouy"
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
num = 0
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
for letter in text:
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
if letter not in vowels:
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
print("consonant", letter)
num += text.lower().count(letter)
Copy link
Owner

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.

Copy link
Owner

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))



sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
return num
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved

text = str(input("Enter a sentence: "))

sanyatonwu marked this conversation as resolved.
Show resolved Hide resolved
Expand Down