-1

I'm trying to count the # of vowels and consonants in an inputted string (without punctuation). Currently, my code is not counting the consonants correctly because I don't know how to return 2 different values from one function...storing in a tuple is the way to go?

sentence = str(input("Enter an English sentence: ")).lower()
   
def vc_counter(sentence):
    '''Check how many vowels and consonants are in the sentence'''
    VOWELS = ('aeiou')
    consonants = ('bcdfghjklmnpqrstvwxyz')
    v_count = 0
    c_count = 0
    for letter in sentence:
        letter = letter.strip('.,!')
        if letter in VOWELS:
            v_count += 1
    return v_count
    for letter in sentence:
        if letter in consonants:
            c_count += 1
        return c_count

print("Total # of vowels in sentence: ", vc_counter(sentence))
print("Total # of consonants in sentence: ", vc_counter(sentence))
Michael Butscher
  • 7,667
  • 3
  • 20
  • 24
szwack96
  • 3
  • 1
  • Right, returning a tuple is the usual way. You could instead also create your own class for storing and return an object of it but this would be overkill here. – Michael Butscher Oct 10 '20 at 16:05
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). We expect you to try to find the answer before posting here. "Python return multiple values" solves the issue. – Prune Oct 10 '20 at 16:08

1 Answers1

0

you can return multiple values in python directly using

return v_count,c_count

in place of

return c_count

at the place where you are calling function vc_counter(sentence), you can write it as

v_count, c_count = vc_counter(sentence)
Pranjal Doshi
  • 374
  • 2
  • 12