Consonant Count

Hi, I’m trying to write out a code on Coderbyte that counts the number of consonants in a string, I know I must be missing something but could someone explain where I’m going wrong? I’ve changed it a few times but at this rate I don’t want to make it any worse :sweat_smile: I’m also not sure what to do with the raw_input? I’ve tried adding “Alphabets” to it but maybe I didn’t do it right?

def ConsonantCount(strParam):

   vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
   strParam = “Alphabets”

   consonant_count = 0

   for char in strParam:
     if char in vowels:
       pass
     else:
       consonant_count += 1
     return consonant_count

print ConsonantCount(raw_input())

Edit: is this any better? I’ve managed to condense it down a bit but it’s still throwing errors back at me? (says consonant_count is not defined)

def ConsonantCount(strParam):

   strParam = “Alphabets”
   consonant_ count = 0
   
   for i in strParam:
     if i not in “aeiouAEIOU”:
       consonant_ count += 1
     return consonant_count

print (“Number of consonants: “ + str(consonant_count))

Your print() statement is outside of the function, meaning that it’s outside of the scope where consonant_count is defined. Additionally, by setting strParam to "Alphabet" (i.e., by assigning it a value inside the function), there’s little point in having it as a parameter; if you wanted to accept user input from within the function, you don’t need to have it as a parameter…

A few minor things

There seems to be a space between consonant_ and count where you declare that variable:

Also, you’ve named your function in PascalCase (where you capitalise every word’s first letter), but you should use snake_case (where you put an underscore between each word). Same goes for strParam; you’ve used camelCase, but should use snake_case…

I hope that helps!

Hi,
Your first try is not too far off.
Your issues appear to be;

  1. your return statement is inside your for loop, so it would only check the first letter then end the function. It should be level with the for line.
  2. the print line.
    Your function call, ConsonantCount should be in brackets (), and raw_input has been replaced by input so the codebyte might not recognise the old method;

print (ConsonantCount(input()))

So, if you tweak those two and then you can remove the strParam = “Alphabets” line (as you’re using user input) and your code should work as you expected.

Hope that helps.

Thank you both! As for the PascalCase, it was the way the code was set up for the challenge before I did anything to it so I wasn’t sure if I was supposed to change it at all.

Same goes for the print statement, I didn’t change where it was indented (or not indented) so I didn’t realise that’s what was throwing off my code, as well as the fact that it didn’t have brackets around it originally (only around raw_input).

It also makes so much sense to me now about where my return function should’ve been so thank you for clarifying! I can see I had the right idea with my code but it’s just little tweaks I had to make :sweat_smile:

P.S. the space in consonant_count wasn’t there in my code, I just wrote it out on here with my iPad and autocorrect got in the way :joy: but helpful to know!

2 Likes